How to load ASCII data into GAUSS and keep variable names

Hi, I am working on a project where I do not have access to atog.exe to convert spss file with variable name to a gauss file. So I was just wondering, how can I load the file with variable names without atog conversion? I would really appreciate the help.

 

 

 

 

1 Answer



0



It sounds like you have an ASCII data file in which the first row contains the variable names and each row after that contains the main data. If this is the case, here is a GAUSS procedure you can use to load the data into GAUSS.

proc (2) = loadAsciiVars(file_name);
   local fh, var_names, data, num_vars, ret;
	
   fh = fopen(file_name,"r");
	
   //Read in first row as a string array
   //Assuming first row contains variable names
   var_names = fgetsa(fh, 1);
	
   //Split string into string array
   var_names = strsplit(var_names);
	
   num_vars = cols(var_names);
   ret = close(fh);
	
   //load entire file as a (num_obs * num_vars) vector
   load data[] = ^file_name;
	
   //reshape data into (num_obs by num_vars) matrix
   data = reshape(data, rows(data)./num_vars, num_vars);
	
   //trim off row that contains variable names
   data = trimr(data, 1, 0);
	
   retp(var_names, data);
endp;

As a quick example, if you had an ASCII file named mydata.asc with these contents:

GDP GNP Inflation Exports
1 2 6 4
2 2 3 1
3 4 6 6
4 7 2 2

you could load the data, using the procedure from above like this:

{ v_names, my_data } = loadAsciiVars("mydata.asc");

After running this code, the GAUSS symbol v_names would be a 1x4 string array containing:

GDP GNP Inflation Exports

and my_data would be a 4x4 matrix containing:

1 2 6 4
2 2 3 1
3 4 6 6
4 7 2 2

aptech

1,773

Your Answer

1 Answer

0

It sounds like you have an ASCII data file in which the first row contains the variable names and each row after that contains the main data. If this is the case, here is a GAUSS procedure you can use to load the data into GAUSS.

proc (2) = loadAsciiVars(file_name);
   local fh, var_names, data, num_vars, ret;
	
   fh = fopen(file_name,"r");
	
   //Read in first row as a string array
   //Assuming first row contains variable names
   var_names = fgetsa(fh, 1);
	
   //Split string into string array
   var_names = strsplit(var_names);
	
   num_vars = cols(var_names);
   ret = close(fh);
	
   //load entire file as a (num_obs * num_vars) vector
   load data[] = ^file_name;
	
   //reshape data into (num_obs by num_vars) matrix
   data = reshape(data, rows(data)./num_vars, num_vars);
	
   //trim off row that contains variable names
   data = trimr(data, 1, 0);
	
   retp(var_names, data);
endp;

As a quick example, if you had an ASCII file named mydata.asc with these contents:

GDP GNP Inflation Exports
1 2 6 4
2 2 3 1
3 4 6 6
4 7 2 2

you could load the data, using the procedure from above like this:

{ v_names, my_data } = loadAsciiVars("mydata.asc");

After running this code, the GAUSS symbol v_names would be a 1x4 string array containing:

GDP GNP Inflation Exports

and my_data would be a 4x4 matrix containing:

1 2 6 4
2 2 3 1
3 4 6 6
4 7 2 2

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