Lesson 15: Examples in SAS Macro

SAS macro are particular useful in repeated data reading, management and output.

Example 1

This example is to rearrangement the data to fit SAS PROC FREQ when the data are presented in a very different format.

In this data set, each tooth of a patient is evaluated by two dentists on four different characters; gingival area stain (GAS), gingival area color (GAC), tooth body stain (TBS) and tooth body color (TBC). Each tooth is evaluated twice by two dentists from two training program. DEntist 'SC' and 'OS' are from one training program and 'AM' and 'IM' are from the other. All the data are categorical. The purpose is to assess the intra-dentist and inter-dentist evaluation agreement on each of the four characters.

From the experiment, the data is recorded in the following manner:


dentist patient replicate tooth GAS GAC TBS TBC

In order to use PROC FREQ to assess the intra-dentist and inter-dentist evaluation agreements, we data should be rearranged as


patient tooth dentist1-replicate1 dent1-rep2 dent2-rep1 dent2-rep2 

for each character GAS GAC TBS and TBC. Once this is done, the agreements are in the contingency tables:

DATA dental;INPUT PAT TOOTH D11 D12 D21 D22;
D1=(D11+D12)/2;D2=(D21+D22)/2;
PROC FREQ; TABLES D11*D12; *Intra-dentist agreement for dentist 1;
PROC FREQ; TABLES D21*D22; *Intra-dentist agreement for dentist 2;
PROC FREQ; TABLES D1*D2;   *Inter-dentist agreement;
RUN;
Here is a sas program that can do it.

OPTION LS=76 NOCENTER NODATE;

%MACRO REARNGE(IN=,DEN=,REPC=,LAB=,OUT=);

DATA &OUT;SET ∈
IF (DC=&DEN AND REP=&REPC); * Only data in this category retained:
GAS&LAB=GAS;TBS&LAB=TBS;    * relabel variables according to DEN-REP;
GAC&LAB=GAC;TBC&LAB=TBC; 
PROC SORT;BY PAT TOOTH;

%MEND;
 
%MACRO TOGETHER(DUMMY=);
%DO I=1 %TO 4;            * There are 4 dentists;
%DO J=1 %TO 2;            * There are 2 replicates;
%LET IJ=%eval(&I*10+&J);  * separate data according to DEN-REP;
%REARNGE(IN=ST0,DEN=&I,REPC=&J,LAB=&IJ,OUT=ST&IJ);
%END;%END;
RUN;
%MEND;

DATA ST0;INFILE 'ST.dat';
INPUT ITEM  $ DEN $ PAT REP TOOTH GAS TBS GAC TBC;
IF DEN='SC' THEN DC=1;   * change dentists' names to numbers;
IF DEN='OS' THEN DC=2;
IF DEN='AW' THEN DC=3;
IF DEN='IM' THEN DC=4;
CARDS;
%TOGETHER(DUMMY=0);

DATA SUMMARY;MERGE ST11 ST12 ST21 ST22 ST31 ST32 ST41 ST42;
BY PAT TOOTH;
PROC PRINT;VAR PAT DEN GAS11 GAS12 GAS21 GAS22 GAS31 GAS32 GAS41 GAS42;
RUN;

Example 2

When SAS testing procedures are used, it usually gives you the p-value under the null hypothesis. Sometimes it is important to compute the power under the alternative hypothesis. Power computations are often difficult by theory, but it can be done easily by simulation. Here are the steps.

  1. Simulate the data set under the alternative hypothesis with given sample size.
  2. Run the SAS procedure with this data set.
  3. Check whether H0 is rejected at the given significance level.
  4. Do this 1000 (or 10,000) times and record the number of rejections.
  5. The power is the proportion of rejection.
  6. For given required power, the sample size can be found by tuning the sample size in the process.

The following macro computes the power in one-way ANOVA.

OPTIONS NOCENTER LS=76 NODATE;

/* For MACRO mix:
   sn=simulation size, m1,....m4= the 4 means and n1,..., n4 =
   the 4 sizes of the 4 groups. sd= error standard deviation,
   seed= seed for random numbers                               */

%MACRO mix(sn=,m1=,m2=,m3=,m4=,sd=,n1=,n2=,n3=,n4=,seed=);
DATA oneway;
DO i=1 TO &sn;                  * simulate data under H1;
 DO j=1 TO  &n1;
     trt='A';
     y=&m1+&sd*rannor(&seed);
     OUTPUT;
END;
 DO j=1 TO  &n2;
     trt='B';
     y=&m2+&sd*rannor(&seed);
     OUTPUT;
END;
 DO j=1 TO &n3;
    trt='C';
    y=&m3+&sd*rannor(&seed);
    OUTPUT;
END;
 DO j=1 TO &n4;
     trt='D';
     y=&m4+&sd*rannor(&seed);
     OUTPUT;
END;
END;                           * End of data simulation;
PROC  SORT; BY i;              * Each i carries one simulated sample;

ODS EXCLUDE  ModelInfo         * see ODS later in this notes; 
             ClassLevels
             Dimensions
             IterHistory
             ConvergenceStatus
             CovParms
             FitStatistics
             Tests3;

ODS OUTPUT tests3=test;       * test3=test => Type III test output;
ODS listing;
PROC MIXED DATA=oneway; BY i;
   CLASS trt;
   MODEL y=trt;               * THE ODS appears before PROC !!!;

DATA test;SET test;
 IF probF<.05 then reject=1;  * The name probF is given in ODS;
 ELSE reject=0;
PROC  FREQ DATA=test;         * For each i reject = either 0 or 1;
 TABLES reject/OUT=a NOPRINT;
%MEND mix;

%MACRO one;
 %DO i=1 %TO 3;
  %LET s=122+&i;
  %mix(sn=1000,m1=0.35,m2=0.50,m3=0.52,m4=0.60,sd=0.19,
       n1=12,n2=30,n3=6,n4=12,seed=&s);
  DATA r&i;SET a;
  IF reject=1;
%END;
%MEND one;

%one;
DATA whole;SET r1 r2 r3;
PROC PRINT;
RUN;
The output of this program is:

Obs    reject    COUNT    PERCENT

 1        1       761       76.1 
 2        1       760       76.0 
 3        1       757       75.7 

The power by non-central F is 0.763.

The instruction of ODS (Output Delivery System) is for special output that is not included in the usual OUTPUT OUT=xx. To find them, use Department computing environment and follow the steps:

  1. => Online Software Documentation
  2. => SAS manuals (UF viewing only)
  3. => SAS/STAT User's Guide (Last item in the list)
  4. => The MIXED Procedure (MIXED as an example)
  5. => Details
  6. => Changes in Output

Return to STA 5106 home page