Monday, January 29, 2018

Hack 7.4 Creating a Temporary Flat File with the TEMP Option of the FILENAME Statement




SAS Programming Professionals,

Did you know that you can use the TEMP option of the FILENAME statement to create a temporary flat file?

You can write to or read from that flat file as long as the FILENAME remains assigned.  I often use the TEMP option to build a program within a program and then execute it.  Here is an example:

/****************************************************/
/* Beginning of the IDENTIFY SAS Macro.             */
/****************************************************/
%MACRO IDENTIFY(SASLIB,DIRNAME);

********************************************************************;
* Pipe the names of data files to the SAS System for processing.   *;
********************************************************************;
filename dircmd pipe "dir /b &DIRNAME\*.txt";

********************************************************************;
* Temporary file to hold the READFLAT SAS Macro call statements.   *;
********************************************************************;
filename holdmacs TEMP;

********************************************************************;
* Process each specific file name, dropping unneeded ones. Format *;
* SAS READFLAT Macro calls for valid data files.                  *;
********************************************************************;
data _null_;

length outline $100;

file holdmacs;

infile dircmd missover length=length;

input bigline $varying200. length;

outline = '%READFLAT(' || "&SASLIB" || ',' || "&DIRNAME" || '\' || trim(left(bigline)) || ');';

put outline;

run;

********************************************************************;
* Include holdmacs, which is now a series of READFLAT SAS Macro    *;
* invocations. This will execute Process_Flat_Files.sas once for   *;
* each “.txt” flat file found in the target data directory.        *;
********************************************************************;
%INCLUDE holdmacs;

/****************************************************/
/* End of the IDENTIFY SAS Macro.                   */
/****************************************************/
%MEND IDENTIFY;

In this very busy example, the first FILENAME statement directs SAS to perform the DIR command on a data directory and output the names of the files in that directory when the DIRCMD filename is accessed. 

The second FILENAME creates a temporary flat file named HOLDMACS

The DATA step reads the data set names passed to the DIRCMD filename statement.  Then, it formats a line of code that is an invocation of the %READFLAT macro.  It then writes that line to the HOLDMACS temporary file.  After the data step has completed executing, the HOLDMACS temporary file looks something like this:

%READFLAT(Q:\data,Q:\system1\datadir\store22_01212015.txt);
%READFLAT(Q:\data,Q:\system1\datadir\store55_01212015.txt);
%READFLAT(Q:\data,Q:\system1\datadir\store64_01212015.txt);
%READFLAT(Q:\data,Q:\system1\datadir\store97_01212015.txt);

So, when the %INCLUDE statement executes, it includes the four macro invocations stored in HOLDMACS, executing them one after another.  Neat trick, eh?  I would bet that you can find a clever use for a TEMP flat file too!

Best of luck in all your SAS endeavors!

---MMMMIIIIKKKKEEEE
(aka Michael A. Raithel)
Author of the new cult classic for computer programmers:  It Only Hurts When I Hit <ENTER>
Print edition:  http://tinyurl.com/z8bzx2e 
Kindle edition: http://tinyurl.com/zypgqa7 

The hack above is an excerpt from the book:  Did You Know That?  Essential Hacks for Clever SAS Programmers

No comments:

Post a Comment