DATA Step, Macro, Functions and more

infile strange text file

Accepted Solution Solved
Reply
Contributor
Posts: 43
Accepted Solution

infile strange text file

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


Accepted Solutions
Solution
‎02-27-2014 05:57 AM
SAS Super FREQ
Posts: 682

Re: infile strange text file

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


All Replies
Solution
‎02-27-2014 05:57 AM
SAS Super FREQ
Posts: 682

Re: infile strange text file

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
;;;;
Contributor
Posts: 43

Re: infile strange text file

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.

Super User
Posts: 6,927

Re: infile strange text file

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

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 6,927

Re: infile strange text file

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;

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 328 views
  • 3 likes
  • 3 in conversation