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

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;

1 ACCEPTED SOLUTION

Accepted Solutions
stat_sas
Ammonite | Level 13

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

6 REPLIES 6
PGStats
Opal | Level 21

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
Reeza
Super User

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?

PGStats
Opal | Level 21

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
Reeza
Super User

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!

stat_sas
Ammonite | Level 13

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;

data_null__
Jade | Level 19

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.

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
  • 6 replies
  • 6432 views
  • 6 likes
  • 4 in conversation