Macro XDIR (from Fraktal SAS Programming)

Zur Navigation springen Zur Suche springen




What it does

This SAS Macro reads entries from a specified OS directory and writes results to the SAS LOG screen.

  • When no directory is specified, a pop-up window is defined and opened to ask the user for the directory name.

Annotated Code

Code executed Function performed

%MACRO xdir(xpath);

Start macro definition with name and positional parameter xpath.


Define branch when parameter xpath is not supplied.

%WINDOW path

Start definition of pop-up window.

irow = 20 rows = 14 icolumn = 30 columns = 64

Specify position and size of window.

#4 @5 'Enter path: ' xpath 40 attr = underline

Specify constant text shown, parameter name to be populated and attributes.


Terminate pop-up window definition statement.

%DISPLAY path;

Open pop-up window.


Finalize branch definition for empty parameter xpath.

filename path pipe "dir /b ""&XPATH.""" lrecl = 256;

Define text data stream with type pipe and logical record length.

data _NULL_;

Initiate data step run group.

length entry $256;

Specify length in byte for character field entry.

infile path length = lrecl;

Open text data stream path.

input entry $varying256. lrecl;

Specify structure of data stream path.

put entry;

Write value from field entry to the SAS LOG screen.


Terminate data step run group.

filename path clear;

Revoke definition of text data stream.

%MEND xdir;

END macro definition with name

How it is done

  1. Upon initialization the presence of the parameter "xpath" is checked.
    1. If not present, a pop-up window is defined and popped-up to ask the user for the path.
  2. The path is then used to create a text data file reference of type pipe.
  3. The created fileref is then read with an appropriate input statement.
    1. No dataset is created by using dummy dataset name _NULL_.
    2. Each text line input is directly output to the log screen as expected.

Special Effects

Generally, SAS is expecting fixed record length data when reading from a text data source. As is the case in our example here, this condition is not met. When reading varying record length data, SAS needs information about the particular record length. This is obtained by using the "length =" option in the infile statement. The variable named after the "=" - sign will hold the length of each line ready for use by the "varying" format in the input statement.

More Special Effects

If you prefer working with interactive tools instead of rough batch runs, then try a slightly modified version of this macro.