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;
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;
You need to use the good old Column version of the INPUT statement
PG
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?
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
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!
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;
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.