Joel Charme
Joel Charme

Reputation: 31

Colons preceding informats in SAS INPUT statement

I am wondering why, if SAS is reading, through an INPUT statement in a data step, only date variable(s), it seems necessary to write a colon before each informat name, while, if the variables' list begins with another type of variable (eg a character variable), colons are not needed in front of informats. To illustrate my question, two small programs (see below). The first one generates invalid data for date (as long as I do not write a colon before mmddyy10., to get INPUT date :mmddyy10.;), while the second does not. Strange, is not it? There is probably some explanation to this, but despite searching through SAS support documentation and forums I am unable to discover what it could be. Any help welcome! Anyway, if I had sooner understood what is at stake I had saved a couple of hours spent at trying to solve this question.

* program #1;
DATA _NULL_;
    INPUT date mmddyy10.;
    DATALINES;
    09/14/2012
    09/15/2012 
    ;
  RUN;
  PROC PRINT; RUN;

* program #2;
  DATA _NULL_;
     INPUT id $ date mmddyy10.;
   DATALINES;
    A 09/14/2012
    B 09/15/2012 
   ;
  RUN;

Upvotes: 1

Views: 597

Answers (1)

Tom
Tom

Reputation: 51566

In your first program you told SAS to read the first 10 characters as a date. But the first 4 characters are all spaces. So you asked it to try to convert ' 09/14/' into a date. I recommend always typing the DATALINES (aka CARDS) statement starting in column one to prevent the auto-indent feature of your editor from causing your lines of data to be indented also.

The colon (:) in front of the inline informat specification tells SAS to use list mode input instead of formatted input. With formatted input SAS reads the number of characters the format specifies. With list mode SAS reads the next field in the line, based on the current delimiter and dsd setting. The width used for the informat is adjusted to match the actual width of the next field in the line.

Example:

data test;
  length name1-name3 $30 ;
  input @1 name1
        @1 name2 $5.
        @1 name3 :$5. 
  ;
cards;
Elizabeth
;

Results:

Obs      name1      name2      name3

 1     Elizabeth    Eliza    Elizabeth

Upvotes: 1

Related Questions