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

dear all,

I have to infile a text file like the one following.

At the moment I just need to have a dataset with one variable and one record for each row of the text file. So that following I can extract something from it (for instance I need all the codes following @I01)

I am not good with infile and input and my attempts have failed. I also tried to transform into excel and the import it into sas but without success.

any help is appreciated

thank you in advance

:R: John Smith, 999-7543,John.Smith@gmail.com

:I:

//Q XYZRTSWPQ NSPRQLQ

//N L 01WOW13

@T

!BRR 08;LIM 20

@01@?30@02

!BRR -100;LIM 0

!LIM 68;BRR 2

@03

@A

Dear

CompanyName

Street Name

Town Name County Post Code

@I01100100100

@I02robqqgzr

@I03100100100

@A

Dear

CompanyNAme

Street Name

Town Name County PostCode

@I01200200200

@I02robfxmal

@I03200200200

@A

1 ACCEPTED SOLUTION

Accepted Solutions
BrunoMueller
SAS Super FREQ

Since the outcome of the program is not exactly clear, I made one up myself, but it might give you a hint on the techniques to use.

With the trailing @ sign in the INPUT statement you can read a line from the file and keep it in the input buffer, so the next INPUT statement will read from the same input buffer.

I made up two record types, on that determines the beginning of a block of lines and one that ends the block of lines to read.

Have a look, it might help you to get started with what you need.

data want;
  infile cards truncover;

 
* read record Type and keep input record ;
 
input
   @
1 recType $4.
   @
  ;

 
* check for beginning of lines to read ;
 
if recType = "@I01" then do;
   
retain readLine;
    readLine = 1;
 
end;

 
* check for end of lines to read ;
 
if recType = "@A" then do;
   
retain readLine;
    readLine = 0;
 
end;

 
* read lines and write to output ;
 
if readLine = 1 then do;
   
input
      @
1 textLine $32.
    ;
    output;
 
end;
cards4;
:R: John Smith, 999-7543,John.Smith@gmail.com
:I:
//Q XYZRTSWPQ NSPRQLQ
//N L 01WOW13
@T
!BRR 08;LIM 20
@01@?30@02
!BRR -100;LIM 0
!LIM 68;BRR 2
@03
@A
Dear
CompanyName
Street Name
Town Name County Post Code
@I01100100100
@I02robqqgzr
@I03100100100
@A
Dear
CompanyNAme
Street Name
Town Name County PostCode
@I01200200200
@I02robfxmal
@I03200200200
@A
;;;;

View solution in original post

4 REPLIES 4
BrunoMueller
SAS Super FREQ

Since the outcome of the program is not exactly clear, I made one up myself, but it might give you a hint on the techniques to use.

With the trailing @ sign in the INPUT statement you can read a line from the file and keep it in the input buffer, so the next INPUT statement will read from the same input buffer.

I made up two record types, on that determines the beginning of a block of lines and one that ends the block of lines to read.

Have a look, it might help you to get started with what you need.

data want;
  infile cards truncover;

 
* read record Type and keep input record ;
 
input
   @
1 recType $4.
   @
  ;

 
* check for beginning of lines to read ;
 
if recType = "@I01" then do;
   
retain readLine;
    readLine = 1;
 
end;

 
* check for end of lines to read ;
 
if recType = "@A" then do;
   
retain readLine;
    readLine = 0;
 
end;

 
* read lines and write to output ;
 
if readLine = 1 then do;
   
input
      @
1 textLine $32.
    ;
    output;
 
end;
cards4;
:R: John Smith, 999-7543,John.Smith@gmail.com
:I:
//Q XYZRTSWPQ NSPRQLQ
//N L 01WOW13
@T
!BRR 08;LIM 20
@01@?30@02
!BRR -100;LIM 0
!LIM 68;BRR 2
@03
@A
Dear
CompanyName
Street Name
Town Name County Post Code
@I01100100100
@I02robqqgzr
@I03100100100
@A
Dear
CompanyNAme
Street Name
Town Name County PostCode
@I01200200200
@I02robfxmal
@I03200200200
@A
;;;;
ciro
Quartz | Level 8

Thank you very much Bruno. It's perfect for what I need. Just I do not understand the use of the second @ in the first input statement.

Kurt_Bremser
Super User

The first @ in the input statement positions the cursor at column 1;

The second, trailing(!) @ tells SAS to keep the contents of the input buffer and not read a new line.

The input statement without the trailing @ switches to the next line of the infile after reading

Kurt_Bremser
Super User

If at first you just want to make sure that you read everything that's in a line, try the following:

%let maxrl=500; * or any number that is appropriate;

data infile1;

infile"xxxx" lrecl=&maxrl truncover;

input whole_line $&maxrl.;

run;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 4 replies
  • 1212 views
  • 3 likes
  • 3 in conversation