BookmarkSubscribeRSS Feed
Steve1964
Obsidian | Level 7

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?

 

 

 

1 REPLY 1
Tom
Super User Tom
Super User

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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 1 reply
  • 318 views
  • 0 likes
  • 2 in conversation