DATA Step, Macro, Functions and more

Importing fixed column text file multiple fixed columns criteria

Reply
Learner
Posts: 1

Importing fixed column text file multiple fixed columns criteria

[ Edited ]

Hi,

 

I have a plain text file I would like to import into SAS.

 

It is a fixed column text file, however there are multiple fixed lengths depending on the type of record.

Notes

1.)  The type of record is indicated by the first number in each length

          -I.E Records that start with 1: Store Indicator                Lines 1 - 10 Record ID, 11-15 Store Name etc...

                 Records that start with 4: Transaction Indicator      Lines 1- 15  FirstName, 15-30 Last Name etc...

                 Records that start with 4 flows over into 2 lines for every record

2.)   Each group of Transactions are separated by a Store Record, however, they vary in then number of transactions

         -I.E. Store Name (Records that start with 1)

                 space

                 Transaction Record #1(Records that start with 4)

                 Transaction Record #1 Continued

                 Transaction Record #2( Records that start with 4)

                 Transaction Record #2  Continued

                 Closing Line (Records that start with 6)

                 space

                 New Store Name Record(1)

                 Transaction Records (4)- Varying #'s

                  Closing Line

 

I have attached a sample text file in a similar format.

 

How would I import a file like this?

 

Thanks

 

Super User
Posts: 6,934

Re: Importing fixed column text file multiple fixed columns criteria

You don't import it.  You have to write a program.  The overall form of the program is to read in the record type, without releasing the current line of data.  Depending on the record type, read in the appropriate variables.  It's actually more complex than that, because  you have to decide what the final data set should look like.  Most likely, you will end up repeating the store-level information for each transaction.  In that case, the overall form of the program would look like this:

 

data want;

infile myfile;

input record_type $1 @;

if record_type='1' then input record_id $ 1-10 store_name $ 11-15 ..........;

retain store_name;

else if record_type='4' then do;

   input FirstName $ 1-15 LastName $ ........................;

   output;

end;

run;

Ask a Question
Discussion stats
  • 1 reply
  • 114 views
  • 3 likes
  • 2 in conversation