East Carolina University
Department of Psychology


SAS Macros


   If you have a batch of SAS code that you wish repeatedly invoke without needing the repeat the code over and over, just enclose the code in a macro statement.  Look at the program below, which I use to generate data for students.  Each student gets a different set of data, I get the solutions.

    To start a macro, use the %MACRO statement followed by the name of the macro.  To end a macro use the %MEND statement followed by the name of the macro.  The DSCRPTV macro below creates and puts into a file a set of data for one student to analyze.  It also puts into a listing file the analysis of that student's data.

    To run a macro simply type a percentage sign followed by the name of the macro.  Look at %name below.  When SAS encounters %name it replaces "%name" with the current text defined as %name.  In this case that is the name of the data file to be created (BLOUSES-01 for the first student, BLOUSES-02 for the second student, and so on).  At the bottom of the program you can see how the text associated with the %name macro is defined and changed.

    The %ST macro is used to insert the name of each student in the title statement for the statistical analysis of that student's data.

    The %OP macro is used as a mirror of the %DESCRIPTIVE macro -- this is for my convenience in using and reusing the names file that assigns a name to each set of data and an associated name for each set of statistical output.  When I want to run a different simulation program I simply append the names file (in which the base name of the output files is "QQ" and then use "replace all" to replace every occurrence of "QQ" with the proper name for that simulation.

    When this program is run in SAS the first two lines of the names file cause SAS to simulate data for the first student, write those data to the file BLOUSES-01, and write the analysis of those data, with the student's name, to the listing.  The next two lines do the same for the second student, and so on.  I post the data files on the Internet with instructions, like this:  Descriptive Statistics Homework.


options pageno=min nodate formdlim='-';
%MACRO DSCRPTV; DATA YS;KEEP Y;N=100;D=UNIFORM(0);
IF D<.2 THEN GOTO U;IF D<.4 THEN GOTO BIMOD;
IF D<.6 THEN GOTO SKEW;IF D<.8 THEN GOTO UNIF;
DO K=1 TO N;Y=round(35+1.5*NORMAL(0));OUTPUT;
FILE "C:\Documents and Settings\Karl Wuensch\My Documents\ECU\SimData\%name.dat"; PUT Y; END; GOTO HELL;
UNIF: DO K=1 TO N;Y=round(30+10*UNIFORM(0));OUTPUT;
FILE "C:\Documents and Settings\Karl Wuensch\My Documents\ECU\SimData\%name.dat"; PUT Y; END; GOTO HELL;
SKEW: IF UNIFORM(0) <.5 THEN S=-1;
ELSE S=1;DO K=1 TO N;Y=round(35+1.0*NORMAL(0));
IF UNIFORM(0) <.2 THEN Y=round(Y+S*8);OUTPUT;
FILE "C:\Documents and Settings\Karl Wuensch\My Documents\ECU\SimData\%name.dat"; PUT Y; END; GOTO HELL;
BIMOD: DO K=1 TO N;IF UNIFORM(0) <.5 THEN Y=round(40+1.5*NORMAL(0));
ELSE Y=round(30+1.5*NORMAL(0));OUTPUT;
FILE "C:\Documents and Settings\Karl Wuensch\My Documents\ECU\SimData\%name.dat"; PUT Y; END; GOTO HELL;
U: DO K=1 TO N;IF UNIFORM(0) <.5 THEN Y=32;
ELSE Y=38; OUTPUT;
FILE "C:\Documents and Settings\Karl Wuensch\My Documents\ECU\SimData\%name.dat"; PUT Y; END;
HELL: PROC UNIVARIATE PLOT;VAR Y;
TITLE1 "Descriptive statistics on the data given to %ST"; %MEND DSCRPTV;
%MACRO OP; %DSCRPTV %MEND OP;
*************************** NAMES FILE FOLLOWS ******************************;
%MACRO ST; Ima Student %MEND ST;
%MACRO NAME; BLOUSES-01 %MEND NAME; %OP run;
%MACRO ST; Sol Xenos %MEND ST;
%MACRO NAME; BLOUSES-02 %MEND NAME; %OP run;

and so on

birds flying

return to Dr. Wuensch's SASLessons PageClick here to return to Dr. Wuensch's SAS Lessons Page.

spider in web
Contact Information for the Webmaster,
Dr. Karl L. Wuensch


This page most recently revised on 24. June 2007.  

Copyright 2007, Karl L. Wuensch - All rights reserved.