So I put together a bit of code to read in a large file. The variable that I oddly need the most is only reading in the first digit. It's a tab delimited file which I have carve out just enough of as a sample to show. The variable I am most concerned about is RCON5597 which is not defined for every row. But where it is I just get that first bit and then nothing else. For example row 25 (obs 23 really) should be 314408 and I'm getting 3.
filename data1222 'rco1222a.txt';
proc import datafile = data1222
out = a1222
dbms = TAB replace;
getnames = yes;
datarow = 3;
run;
If you are going to force SAS to GUESS how to read a file you could at least ask it to check the whole file before making its guesses.
Add this statement to the PROC IMPORT step
guessingrows=max;
If you must read data with Proc Import it is a good idea to use the GUESSINGROWS option set to a large value.
Otherwise SAS only uses the first few rows of the file to determine types and properties of variables, often 20 rows.
If you look at your file the first row with a value for that variable is row 25. So SAS defaulted to treated the value as a one character so you could see something.
proc import datafile = data1222 out = a1222 dbms = TAB replace; getnames = yes; datarow = 3; guessingrows=max; run;
Proc Import guesses for each run of the program. So if you read different files that are supposed to be of the same structure you can get different variable types, lengths and in some cases variable names. So if you have multiple files it is a good idea to write a data step to read text files. Proc Import will write one, which you should see in the log. You could copy that from the log, paste into the editor and clean up such as removing line numbers and specifying desired informats. Most of the Format statements generated won't be needed except for date, time or datetime values.
Another advantage of the data step is you can labels, so things like the second row of your file are associated with the variable.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.