I use input and infile statements with option in data step to input external data.
In first case, the external data are in-stream data lines and the code are
data test1;
infile datalines missover;
input num 5.;
datalines;
1
22
333
4444
5555
;
run;
tes1 has three observations,
1
22
333
4444
55555
In Second case ,the external data is in d:\mydata\x.txt and and the code are
data test;
infile 'd:\mydata\x.txt' missover;
input num 5.;
run;
test has its observation as following
.
.
.
.
5555
which is different from those in test1 completely. Why?
SAS will pad in-line data records to an exact multiple of 80 characters (card images). That is why the behave differently.
But the real issue is that your INPUT statement is wrong. You are asking it to read EXACTLY five characters. If the line is shorter than five characters long (or has less than five character left before the end-of-line) then the INPUT statement will throw away those characters and move to the next file to find something that will satisfy what you wanted. Fortunately it only moves to the next line once (or else you would only get two observations). So the first two iterations of your data step both consume two lines from your text file.
You could add the TRUNCOVER option to the INFILE statement to change how it behaves when there are not enough characters on the line to satisfy the INPUT statement.
Or change the INPUT statement so it will not try to read so many characters. You could use LIST mode. Either by removing the informat (SAS does need special instructions for reading normal numbers). Or adding the : modifier before the informat specification.
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.