DATA Step, Macro, Functions and more

Import Fixed Length File

Accepted Solution Solved
Reply
Super User
Posts: 17,826
Accepted Solution

Import Fixed Length File

I have a fixed length file as follows and am having trouble reading it in.

This is what the data looks like

XXXXXXX0101APR201431MAR201515OCT2012     1H0H0H0A
XXXXXXX0201APR201431MAR201525MAY2009     0H0H0H0B
XXXXXXX0301APR201431MAR201510SEP1945     1H0H0H0C
XXXXXXX0401APR201431MAR201515SEP1953     0H0H0H0B

My code, which doesn't work. It reads in the ID's but then all the other fields are missing, when I can see them in the text file.

data WANT;

    infile "Sample.txt" lrecl=54 truncover firstobs=1;

    informat     ID                                 $9.

                DATE1                             date9.

                DATE2                             date9. 

                DATE3                             date9.    

                DATE4                             date9.

                FLAG1                            $1.

                LOCATION                         $6.

                FLAG2                            $1. ;

    input     ID $                                   

            Date1                               

            Date2                                

            Date3                        

            Date4            

            Flag1    $                       

            Location $               

            Flag2   $           

            ;

run;


Accepted Solutions
Solution
‎07-21-2014 01:21 PM
Trusted Advisor
Posts: 1,204

Re: Import Fixed Length File

Hi,

Not sure about the exact positioning of the variables in text file, this is based on datalines may be helpful in importing text file.

data WANT;

    infile datalines;

input ID $7. Date1 8-9 Date2 date9.  Date3 date9. date4 date9. @39 Flag1 $1.  @40 Location $6.  @46 Flag2 $1.;

datalines;

XXXXXXX0101APR201431MAR201515OCT2012  1H0H0H0A

XXXXXXX0201APR201431MAR201525MAY2009  0H0H0H0B

XXXXXXX0301APR201431MAR201510SEP1945  1H0H0H0C

XXXXXXX0401APR201431MAR201515SEP1953  0H0H0H0B

;

run;

View solution in original post


All Replies
Respected Advisor
Posts: 4,646

Re: Import Fixed Length File

You need to use the good old Column version of the INPUT statement

http://support.sas.com/documentation/cdl/en/lestmtsref/63323/HTML/default/viewer.htm#n13ejk9swz5vrbn...

PG

PG
Super User
Posts: 17,826

Re: Import Fixed Length File

I've tried that as well, but couldn't get it to work with the Date9 values. How do I specify the format with those?

Respected Advisor
Posts: 4,646

Re: Import Fixed Length File

Now I remember why I never use it. Column input is limited to standard variable types, and that doesn't include dates (?), and doesn't support informats. In other words, it is fairly useless.

Try this instead:

data WANT;

infile "sample.txt" truncover;

    input   @1 ID $9.                                 

            @10 Date1 date9.                           

            @19 Date2 date9.

            @28 Date3 date9.

            @37 Date4 ?? date9.         

            @46 Flag1 $1.                      

            @47 Location $6.

            @53 Flag2 $1.;

run;

PG

PG
Super User
Posts: 17,826

Re: Import Fixed Length File

I fully agree, but we're required to submit it this way. Mixing and matching confuses me, but I got it to work.

Thanks all!

Solution
‎07-21-2014 01:21 PM
Trusted Advisor
Posts: 1,204

Re: Import Fixed Length File

Hi,

Not sure about the exact positioning of the variables in text file, this is based on datalines may be helpful in importing text file.

data WANT;

    infile datalines;

input ID $7. Date1 8-9 Date2 date9.  Date3 date9. date4 date9. @39 Flag1 $1.  @40 Location $6.  @46 Flag2 $1.;

datalines;

XXXXXXX0101APR201431MAR201515OCT2012  1H0H0H0A

XXXXXXX0201APR201431MAR201525MAY2009  0H0H0H0B

XXXXXXX0301APR201431MAR201510SEP1945  1H0H0H0C

XXXXXXX0401APR201431MAR201515SEP1953  0H0H0H0B

;

run;

Respected Advisor
Posts: 3,777

Re: Import Fixed Length File

Reeza wrote:

I have a fixed length file as follows and am having trouble reading it in.

This is what the data looks like

XXXXXXX0101APR201431MAR201515OCT2012    1H0H0H0A
XXXXXXX0201APR201431MAR201525MAY2009    0H0H0H0B
XXXXXXX0301APR201431MAR201510SEP1945    1H0H0H0C
XXXXXXX0401APR201431MAR201515SEP1953    0H0H0H0B

My code, which doesn't work. It reads in the ID's but then all the other fields are missing, when I can see them in the text file.

data WANT;

    infile "Sample.txt" lrecl=54 truncover firstobs=1;

    informat     ID                                 $9.

                DATE1                             date9.

                DATE2                             date9.

                DATE3                             date9.   

                DATE4                             date9.

                FLAG1                            $1.

                LOCATION                         $6.

                FLAG2                            $1. ;

    input     ID $                                  

            Date1                              

            Date2                               

            Date3                       

            Date4           

            Flag1    $                      

            Location $              

            Flag2   $          

            ;

run;

The input statement as you have written is using LIST input.  You probably want to use FORMATTED input with a column pointer here an there.

☑ This topic is SOLVED.

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

Discussion stats
  • 6 replies
  • 231 views
  • 6 likes
  • 4 in conversation