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");
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
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$+"'"; endif; endp;