SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
hellohere
Pyrite | Level 9

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%

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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.

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26

Please see the section entitled "Line Pointer Controls" here. Essentially using a / or the # operator lets your INPUT statement read from multiple lines.

--
Paige Miller
ballardw
Super User

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.

hellohere
Pyrite | Level 9

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;

 

Tom
Super User Tom
Super User

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;

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


Register now!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1897 views
  • 0 likes
  • 4 in conversation