SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
japfvg
Fluorite | Level 6

Dear experts:  

 

I have this issue, I have a text file with info like the following (unstructured data):

 

 

Report Title
Report Subtitle
Dates, etc, etc
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
-------------------------------------------------------
A0100 Info about something
      important that is needed
      to capture
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
..................................................................

 

 

The information I'm looking for is located after the "A0100" and continues for the next 2 lines ("Info about something important that is needed to capture"), sometimes it's 6 lines or 8 (it may vary) but the message always starts with the "A0100" and also starts at position 7 and ends at 31 and is needed in one field called "message" in one record.

 

If there is another message below, it will be in another record (same field).

 

Any ideas on how to get this?

 

Thanks in advance.

 

Kind regards!

 

FerV

1 REPLY 1
Tom
Super User Tom
Super User

Read ahead.

 

As long as you can tell by looking at some part of the next line (or current line) that it is an continuation.

 

In your case is looks like the continuation lines have blanks in the first field so perhaps something like this.

data want;
  infile 'myfile.txt' truncover end=eof;
  length id $5 rec 8 fullstring $300 ;
  input id $5 fullstring $100. ;
  do rec=1 by 1 until (eof or _infile_^=: '     ');
    if not eof then do;
      input @@ ;
      if _infile_ ^=: '     ' then do;
        input @6 nextstring $100.;
        fullstring=catx(' ',fullstring,nextstring);
      end;
    end;
  end;
  drop nextstring;
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
  • 1 reply
  • 437 views
  • 1 like
  • 2 in conversation