Monday, April 4, 2016

Hack 2.14 Using the DATALINES and INFILE Statements to Test Input From External Files

SAS Programming Professionals,

Did you know that you can use the INFILE statement with the DATALINES statement?

This is handy in situations where you would like to test the results of inputting an external file by including some sample input data beneath the DATALINES statement.  Once you have vetted your INPUT statements, you would:

  • Code a FILENAME statement before the data step that points to the data you intend to process, 
  • Change the fileref that is coded in your INFILE statement from “datalines” to what is specified on the FILENAME statement,
  • Remove the DATALINES statement and the sample input data from the DATA step.

 Observe this simple example: 

data didyouknow;
infile datalines missover;
input firstname $ age;
Michael 35
Mark 25

proc print;

In the example, the INFILE statement specifies DATALINES as the fileref SAS is to use for inputting data to the DATA step.

The output from executing this example looks like this: 


 Note that the MISSOVER option is absolutely necessary in this example.  And, it can only be specified on an INFILE statement.  The MISSOVER option is necessary because the sample data does not always contain a value for AGE.  Without the INFILE statement and the MISSOVER option, SAS would not know it needs to go to a new line when the INPUT statement reached past the end of the current line.  So, when it got to the second line of input, it would attempt to go to the third line and read “Mark” into the AGE variable, resulting in a pesky note in the log: 

NOTE: Invalid data for age in line 24 1-4.

RULE:      ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+---

24         Mark 25

firstname=Minerva age=. _ERROR_=1 _N_=2


 This means that you would not have been able to adequately test your program with a DATALINES statement without the INPUT statement.

If you prefer to use the CARDS statement to read inline data fear not; it can also be used the same way!  Simply specify CARDS as the fileref in the INFILE statement.  It is always nice to have choices!

Best of luck in all of your SAS endeavors!
(aka Michael A. Raithel) 

Excerpt from the book:  Did You Know That?  Essential Hacks for Clever SAS Programmers

I plan to post each and every one of the hacks in the book to social media on a weekly basis.  Please pass them along to colleagues who you know would benefit.