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

Hello, 

 

Trying to read in a .txt file that stores information in sections, broken into lines like a paragraph, but the number of lines per section is unknown. There are two blank lines between each section, I really only need the first two lines from each section. Example:

 

  1. .TXT TITLE LINE
  2. Job_Code1
  3. Job_script information
  4. Job_description_1
  5. _blank
  6. _blank
  7. Job_Code2
  8. Job_script information
  9. Job_description_1
  10. Job_description_2
  11. _blank
  12. _blank
  13. Job_Code3
  14. Job_script information
  15. Job_description_1

 

This is the code I have so far which reads by line with a line specifier, but the problem is - in this case - job_code2 spans over 4 lines while the rest spans over 3. My code is very simple and I was hoping to keep it neat, maybe using a macro to define the line specifier but I don't have much experience with macros. Any ideas? My code to read by fixed number of lines:

data want;
infile myText firstobs=2 n=5 truncover;
input Job_code $250. #2
         Job_script $250. #5;
run;

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

The easiest way is probably to crawl through line by line instead of defining a block of lines.  The INPUT logic might read:

 

input job_code $250.

 / job_script $250.

 / test $10.

 / test $10.;

if test > ' ' then input /;

else input;

drop test;

 

Of course if there are other variations, you will need to adjust again.  But that can be done ... you just need a good grasp of what is  in the data.

 

Good luck.

View solution in original post

2 REPLIES 2
Astounding
PROC Star

The easiest way is probably to crawl through line by line instead of defining a block of lines.  The INPUT logic might read:

 

input job_code $250.

 / job_script $250.

 / test $10.

 / test $10.;

if test > ' ' then input /;

else input;

drop test;

 

Of course if there are other variations, you will need to adjust again.  But that can be done ... you just need a good grasp of what is  in the data.

 

Good luck.

mhouse
Fluorite | Level 6

This worked perfectly for my  application. Thank you so much! Thank you for the quick help too.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

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