Reading Text Data into SAS

Reply
New Contributor
Posts: 2

Reading Text Data into SAS

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;

 

Super User
Posts: 6,785

Re: Reading Text Data into SAS

Posted in reply to Lejink222

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.

 

New Contributor
Posts: 2

Re: Reading Text Data into SAS

Posted in reply to Lejink222

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

Super User
Posts: 6,785

Re: Reading Text Data into SAS

Posted in reply to Lejink222

This data doesn't line up in the columns that your INPUT statement expects.  Is this actually what your data looks like?

Valued Guide
Posts: 597

Re: Reading Text Data into SAS

Posted in reply to Lejink222
  1.  Firstly check your data and define the column input properly. In your input statement shows that you have only one space between each variable, if your data is delimited by space then you don't need column input style. Simply mention dlm=" " in Infile statement.
  2. Use appropriate informat to read your data.
  3. If you apply custom format then you data must support the format. In your case delay and date values looks entirely different. Are delays are actually days. Is your custom format is defined in a way to format day values and dates values.
Thanks,
Suryakiran
Super User
Posts: 10,280

Re: Reading Text Data into SAS

Posted in reply to Lejink222

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Super User
Posts: 8,124

Re: Reading Text Data into SAS

Posted in reply to Lejink222

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
;
Ask a Question
Discussion stats
  • 6 replies
  • 168 views
  • 0 likes
  • 5 in conversation