Get file with record over 4 lines(sample below)... How to read-in over lines (var names do not matter, just var1/2/3/...).
GAS 6246.00 2203 5111
1135 18.17%
2204 6134
112 1.79%
PORK 12.40 2203 11215
1185 9.56%
2205 14210
-1810 -14.60%
IF every record takes the same number of lines in the source then write a data step to read the data and your INFILE statement uses the N= option to specify that number. Then use the # in the INPUT statement to indicate which line to read specific variables from.
data example; infile datalines n=4; input var1 $ varw var3 var5 #2 var6 var7 :commax. #3 var8 var9 #4 var10 var11 :commax. ; format var7 var11 percent8.2; datalines; GAS 6246.00 2203 5111 1135 18.17% 2204 6134 112 1.79% PORK 12.40 2203 11215 1185 9.56% 2205 14210 -1810 -14.60% ;
If your data has varying number of lines per record this likely won't work.
Please see the section entitled "Line Pointer Controls" here. Essentially using a / or the # operator lets your INPUT statement read from multiple lines.
IF every record takes the same number of lines in the source then write a data step to read the data and your INFILE statement uses the N= option to specify that number. Then use the # in the INPUT statement to indicate which line to read specific variables from.
data example; infile datalines n=4; input var1 $ varw var3 var5 #2 var6 var7 :commax. #3 var8 var9 #4 var10 var11 :commax. ; format var7 var11 percent8.2; datalines; GAS 6246.00 2203 5111 1135 18.17% 2204 6134 112 1.79% PORK 12.40 2203 11215 1185 9.56% 2205 14210 -1810 -14.60% ;
If your data has varying number of lines per record this likely won't work.
What if the info is in a file?! I tried below, it goes wack thou
data example;
infile "C:\Users\24891\Desktop\sasOUT\20220301_copy.csv" n=4;
input var1 $ varw var3 var5
#2 var6 var7 :comma4.
#3 var8 var9
#4 var10 var11 :comma4.
;run;quit;
Whether a file or as in-line data it should work the same.
How does it not work?
Does the actual file not follow the pattern of your example lines?
Why does the file have a CSV extension? The sample lines you showed look nothing like a CSV file there weren't any commas on any of the lines.
To look the beginning of the file run a simple data step to dump the first lines to the SAS log.
data _null_;
infile "C:\Users\24891\Desktop\sasOUT\20220301_copy.csv" obs=5;
input;
list;
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.