DATA Step, Macro, Functions and more

Looking for help with Reading In a Text File

Reply
Occasional Contributor Caz
Occasional Contributor
Posts: 5

Looking for help with Reading In a Text File

Hi,

I have a file that reads more like a report and need to create some BASE SAS to pick up the fields we are interested in. I have created a dummy example of the type of format that I have with 1 example but then it repeats in this format with the Text, then the next record. (see below)

I would like to be able to get a file with 1 row per record that in that one row, shows the fields

Recordstart

Jo Blogs, 1 High St, London

Pat Post, Flat 123, The Shard, London

199999.99 EUR

EUR 10.00

ABC Processing Centre, Whitechapel, London

Dummy Example of Format of file
Lots of Text, Lots of Text, Lots of Text.........................

Lots of Text, Lots of Text, Lots of Text for a few rows....

Format = Recordsart

Identifier = 123.ext.out

Customer1 =

               Jo Blogs

               1 High St

               London

              

Customer2 =

               Pat Post

               Flat 123

               The Shard

               London

Reference = 12345

Amount = 199999.99 EUR

Version = 132                                 Check = Yes

System = FIN                                 Passed = Yes    

Block4:

Field1: EUR 10.00

Field2: ABC Processing Centre

Whitechapel

London

Field3: ABCXXX

Has anyone had experience of such a file and can assist?

Caz    

Super Contributor
Posts: 578

Re: Looking for help with Reading In a Text File

assuming everything is consistent (which might be a big assumption).

data want(keep=rec);

infile fid;

length

     txt $100.

     rec $100.

;

retain rec;

input txt & $100.;

if substr(txt,1,8)='Customer' then do; /*handles customer1 and customer2*/

     rec='';

     input txt & $100.; /*name*/

     rec=compress(txt);

     input txt & $100.; /*address*/

     rec=catx(', ',rec,txt);

     input txt & $100.; /*city*/

     rec=catx(', ',rec,txt);

     output;

     end;

else if substr(txt,1,9)='Amount = ' then do;

     rec=substr(txt,10);

     output;

     end;

else if substr(txt,1,7)='Field1:' then do;

     rec=substr(txt,9);

     output;

     end;

else if substr(txt,1,7)='Field2:' then do;

     rec=substr(txt,9);

     input txt & $100.; /*address*/

     rec=catx(', ',rec, txt);

     input txt & $100.; /*city*/

     rec=catx(', ',rec, txt);

     output;

     end;

run;

Ask a Question
Discussion stats
  • 1 reply
  • 143 views
  • 0 likes
  • 2 in conversation