Writing a numeric value into a CSV

Hi there,

How do I write a numeric value into a CSV in GAUSS 17?

Here's a quick example:

x = 10;
my_file = fopen("test.csv", "w");
write_numeric_value_to_csv(my_file, x);

Is there an easy way to do this in GAUSS 17? If there is no direct way to do this, is there a way to transform a numeric value into a string? If so, then I could use the function fputs(my_file,x_string).


1 Answer


Writing CSV in GAUSS 19 or newer

In GAUSS 19 or newer, the saved function allows you to conveniently save data in CSV, DAT or XLS/XLSX files like this:

x = rndn(10, 3);

// Create CSV with headers X1, X2 and X3
saved("myfile.csv", x, "X"); 

You can also pass in a string array with the exact column headers, if you want something more specific than X1, X2...XP. The file extension will tell GAUSS which type of file to create.

Writing CSV files in GAUSS 17 and 18

I created a simple GAUSS procedure that you can use for versions 17 and 18 below. You can combine many of the steps to create a shorter procedure, but I made it explicit to make it easier to understand and follow.

x = rndn(10,3);

write_csv("myfile.csv", x);

proc (0) = write_csv(fname, X);
    local fh, ret;

    // Turn matrix into string array
    X = ntos(X);

    // Combine each row of X into
    // a single column with commas
    // separating the numbers
    X = strjoin(X, ",");

    // Add a "newline" at the end of each row
    X = X $+ "\n";

    // Open file for writing
    fh = fopen(fname, "w");

    // Write string data
    ret = fputs(fh, X);

    // Close file handle
    call close(fh);

    // Check to see if all rows were written
    if ret != rows(X);
        // Print error message
        errorlogat "write_csv: "$+ntos(ret)$+" of "$+ntos(rows(X))$+" rows written to '"$+fname$+"'";


You must login to post answers.

Have a Specific Question?

Get a real answer from a real person

Need Support?

Get help from our friendly experts.

Try GAUSS for 14 days for FREE

See what GAUSS can do for your data

© Aptech Systems, Inc. All rights reserved.

Privacy Policy