1
votes

This is a question from the exercises and projects for the little sas book regarding Ch. 3 Exercise 10. The questions asks us to read date as a MMDDYY8. input and print it in MMDDYY10. format. I can read from an external file using infile but using dataline results in invalid data. Thanks in advance.

    data sample;
    OPTIONS YEARCUTOFF = 1950;
    INPUT date MMDDYY8.;
    DATALINES;
    01/01/1920
    ;
    run;
    proc print data = sample;
    format date MMDDYY10.;
    RUN:

SAS log reads :

NOTE: Invalid data for date in line 173 1-12.
RULE:      ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0

173 CHAR   ..01/01/1920

 date=. _ERROR_=1 _N_=1
 NOTE: The data set WORK.SAMPLE has 1 observations and 1 variables.
 NOTE: DATA statement used (Total process time):
       real time           0.00 seconds
       cpu time            0.02 seconds
1
You are not showing all of the LOG. There should be two more lines labeled ZONE and NUMR. These will tell you the values (in HEX) for the two characters in columns 1-2. Shown as ".." on the line labeled CHAR. - data _null_
check the data values in the datalines. Are any of them indented with two leading spaces ? - Richard
Ty for the replies. It is as @Richard described. Turns out I was indenting the datalines with tabs for readability like a OOP language. That explains the two ".." characters. - ImTheRealOne

1 Answers

3
votes

Short answer: Use a colon (:) in your input statement to apply an informat.

options YEARCUTOFF = 1950;
data sample;
input date:MMDDYY8.; /* << use : to apply informat */
datalines;
01/01/1920
;
run;

Longer answer:

Regular list input (no modifiers between the variable name and informat) requires that data is in standard numeric or character format. If you want to import any kind of 'special' data (like formatted dates, or multiple embedded blanks or delimiters) you will need modifiers - such as :, ~ or & to read them correctly.

A great explanation is available here, and more info is available here.

Other hints:

  • Options are global statements, they will parse correctly within a datastep but it looks strange, better to keep them in open code!
  • always use semicolon to terminate a statement (not RUN:)
  • try and be consistent with your case (upper / lower), SAS isn't case sensitive but the person who reads your code might be!