BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ArtemisFowl
Calcite | Level 5

Hello,

When reading a .txt file into SAS, I have trouble figuring out exactly why something works and something else doesn't.

For instance, I have a text file with this content:

1---+----10---+----20---+---
01/05/1989    Rick      11
12/25/1987    Sue       13
01/05/1991    Sally     9

If I run the code

data work.family;

   infile 'C:\test.txt';

   input @1 birth_date mmddyy10.

         @15 first_name $5.

         @25 age 3.;

run;

then I get a dataset with only one row, namely

10597 Rick .

So the date and name turns out alright, but the age doesn't.

If I include the "truncover" option, then the output turns out alright, but I have no idea why.

Kind regards,

Rasmus.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

You told it to try to read past the end of the line.  The first row has only 26 characters and your input statement tried to read 27 so it flowed over to the next line to get more characters. So the first pass through the data step "eats" the first two lines and results in the observation you see.  On the second pass you again tried to read past the end of the line so it tried to get more again, but this time there were no more lines so the data step stopped without writing the observation for Sally because you read past the end of the input file.

TRUNCOVER is the best way to fix this.

You can also add : prefix to your informats.  This will tell SAS to stop looking for characters when it hits a space or an end of line.

data work.family;

   infile 'C:\test.txt';

   input @1 birth_date : mmddyy10.

         @15 first_name : $5.

         @25 age : 3.

   ;

run;

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

You told it to try to read past the end of the line.  The first row has only 26 characters and your input statement tried to read 27 so it flowed over to the next line to get more characters. So the first pass through the data step "eats" the first two lines and results in the observation you see.  On the second pass you again tried to read past the end of the line so it tried to get more again, but this time there were no more lines so the data step stopped without writing the observation for Sally because you read past the end of the input file.

TRUNCOVER is the best way to fix this.

You can also add : prefix to your informats.  This will tell SAS to stop looking for characters when it hits a space or an end of line.

data work.family;

   infile 'C:\test.txt';

   input @1 birth_date : mmddyy10.

         @15 first_name : $5.

         @25 age : 3.

   ;

run;

ArtemisFowl
Calcite | Level 5

Thanks, Tom.

That makes a lot of sense.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 2 replies
  • 896 views
  • 2 likes
  • 2 in conversation