I'm trying to use the getHeaders function, but I get this error:
G0025 : Undefined symbol: 'getHeaders'
I'm currently using GAUSS 17. I've looked in the Language Reference and User Guide PDFs and haven't found any reference to the "getHeaders" function. I did find the function "dbGetTableHeaders", but I don't think that does the same thing.
getHeaders was introduced in GAUSS version 18. It allows a simple method to get the variable names from a variety of dataset file types such as: CSV, Excel, HDF5 and GAUSS, SAS, STATA and SPSS datasets. For older versions of GAUSS you have a few options.
Getting headers from a CSV file
If you have a tabular dataset where the first row is the headers (or variable names), you can use
csvReadSA to read only the first row as a string array like this:
// Read all columns from the row range 1 to 1 // as a string array hdrs = csvReadSA("my_csv_dataset.csv", 1|1);
Getting headers from a GAUSS dataset
Prior to GAUSS 18 there were 2 functions to get the variable names from a GAUSS dataset. Both of them had issues that made them less than optimal. The first function is
getname. It takes in the name of a dataset file as a string and returns the header names as a character array.
A character array is a numeric matrix with character data inside of it. In the good old days this had some advantages, however, now we recommend against using character arrays in most cases (use string arrays instead).
// Read variable names from a GAUSS dataset as a character array hdrs_ca = getname("my_gauss_dataset.dat"); // To view printed output of a character array // as text, you need to use the $ operator print $hdrs_ca;
You can convert a character array to a string array by using the string combine operator
// 1. Read variable names from a GAUSS dataset as a character array // 2. Combine an empty string with the character array to convert // the return from 'getname' to a string hdrs_sa = "" $+ getname("my_gauss_dataset.dat"); // Print the string array print hdrs_sa;
The second function from GAUSS 17 and older is
getnamef takes in a file handle to a GAUSS dataset and returns a string array of the variable names.
// Get a file handle fh = dataOpen("my_gauss_dataset.dat", "read"); // Read headers as a string array hdrs_sa = getnamef(fh); // Close the file handle close(fh);
GAUSS 18 has quite a bit of new functionality to make it easier to work with your data.
This is great, thanks!