Hello, I am running into a lot of problems trying to read a text file on my computer into SAS. There are 6 variables in the dataset and I created a user defined format for one of the variables. I've tried many variations of the code below, but I continue to get errors. Can someone help me troubleshoot?
The informat step is what it seems to have a lot of trouble with, because it underlines the 5 and gives me this error:
ERROR 22-322: Syntax error, expecting one of the following: a name, -, ;,
DEFAULT, _ALL_, _CHARACTER_, _CHAR_, _NUMERIC_.
ERROR 76-322: Syntax error, statement will be ignored.
data hospital;
infile hosp truncover;
informat date date7. 5-12;
format date delay delaytime.;
input number 1-4
date $ 5-12
Departure $ 13-16
Arrival $ 17-20
Type $ 21-34
Delay 35-36 ;
run;
What actually is in the data where you expect to find DATE? I ask only because you use an informat of DATE7 yet there are 8 characters in columns 5 through 12.
At any rate, if DATE7 is the correct informat, there are two changes to make.
In the INFORMAT statement, remove the column designations:
informat date date7. ;
And in the INPUT statement, remove the instructions. Let the INPUT statement apply the previously assigned informat:
input number 1-4
@5 date
departure $ 13-16
............
;
There are other, similar ways to accomplish the same thing.
This is the text file I have I am working with.
114 01MAR08 LGA LAX Domestic 8
202 01MAR08 LGA ORD Domestic -5
219 01MAR08 LGA LON International 18
622 01MAR08 LGA FRA International -5
132 01MAR08 LGA YYZ International 14
271 01MAR08 LGA PAR International 5
302 01MAR08 LGA WAS Domestic -2
114 02MAR08 LGA LAX Domestic 0
202 02MAR08 LGA ORD Domestic 5
219 02MAR08 LGA LON International 18
622 02MAR08 LGA FRA International 0
132 02MAR08 LGA YYZ International 5
271 02MAR08 LGA PAR International 4
302 02MAR08 LGA WAS Domestic 0
114 03MAR08 LGA LAX Domestic -1
202 03MAR08 LGA ORD Domestic -1
219 03MAR08 LGA LON International 4
622 03MAR08 LGA FRA International -2
132 03MAR08 LGA YYZ International 6
271 03MAR08 LGA PAR International 2
302 03MAR08 LGA WAS Domestic 5
114 04MAR08 LGA LAX Domestic 15
202 04MAR08 LGA ORD Domestic -5
219 04MAR08 LGA LON International 3
622 04MAR08 LGA FRA International 30
132 04MAR08 LGA YYZ International -5
271 04MAR08 LGA PAR International 5
302 04MAR08 LGA WAS Domestic 7
114 05MAR08 LGA LAX Domestic -2
This data doesn't line up in the columns that your INPUT statement expects. Is this actually what your data looks like?
When posting text where the horizontal formatting (spacing) is important (code, logs, input data), always (and I mean ALWAYS) use the {i} button to open the appropriate window. The main posting window converts to HTML, skewers spacing and replaces certain characters and character sequences. For SAS code, use the "little running man" to get nice enhanced-editor-like coloring.
You cannot combine column based and informat. You will need to adjust how you read the date variable to not use column ranges.
format date yymmdd10. delay delaytime.;
input
number 1-4
@5 date :date.
Departure $ 13-16
Arrival $ 17-20
Type $ 21-34
Delay 35-36
;
Looks like your data is space delimited. If there a no missing values then just define your variables. Add any NEEDED informats and/or formats. And use a simple INPUT statement that just lists the variables.
If you define the variable in the same order they appear in the data then you can used a positional variable list in the INPUT statement.
data want;
infile cards truncover;
length number 8 date 8 Departure $4 Arrival $4 Type $14 Delay 8;
informat date date.;
format date yymmdd10. delay delaytime.;
input number -- delay;
cards;
114 01MAR08 LGA LAX Domestic 8
202 01MAR08 LGA ORD Domestic -5
219 01MAR08 LGA LON International 18
622 01MAR08 LGA FRA International -5
;
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.
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.