Presentation is loading. Please wait.

Presentation is loading. Please wait.

Using Macros to Solve the Collation Problem

Similar presentations


Presentation on theme: "Using Macros to Solve the Collation Problem"— Presentation transcript:

1 Using Macros to Solve the Collation Problem
STAT 541 Using Macros to Solve the Collation Problem ©Spring 2012 Imelda Go, John Grego, Jennifer Lasecki and the University of South Carolina

2 The term collate refers to collecting or arranging (pages) in proper order. SAS provides the convenience of BY-group processing in many of its procedures. BY-group processing produces output for the different groups of interest, but the default output may not be in the desired order.

3 Example PROC SORT DATA=MASTER; BY SCHOOL;
PROC PRINT DATA=MASTER; BY SCHOOL; PAGEBY SCHOOL; VAR SSN LAST FIRST MI EXEMPT; TITLE ’Student Names and Exemption Status’; PROC MEANS DATA=MASTER; BY SCHOOL; VAR GPA; TITLE ’Average GPA’; PROC FREQ DATA=MASTER; BY SCHOOL; TABLES STANINE; TITLE ’Distribution of Reading Test Stanines’; PAGEBY prints a new page. A stanine is a standardized 9-point scale with mean=5 and sd=2.

4 The preceding example will produce output by school per procedure.
What if the desired output is to have all the output from each school together across procedures? This can be easily solved using a number of different strategies.

5 A Solution %MACRO REPORTS (BEGIN, END); %DO INDEX=&BEGIN %TO &END; OPTIONS PAGENO=1; DATA SCHOOL&INDEX; SET MASTER; IF SCHOOL=&INDEX; PROC PRINT DATA=SCHOOL&INDEX; BY SCHOOL; VAR SSN LAST FIRST MI EXEMPT; TITLE ’Student Names and Exemption Status’; PROC MEANS DATA=SCHOOL&INDEX; VAR GPA; TITLE ’Average GPA’; PROC FREQ DATA=SCHOOL&INDEX; TABLES STANINE; TITLE ’Distribution of Reading Test Stanines’; %END; %MEND REPORTS; **THE FOLLOWING STATEMENT GENERATES REPORTS FOR 50 SCHOOLS; %REPORTS(1,50) For the purpose of providing an example, the schools are numbered or indexed conveniently from 1 to 50. A data set created for each school is used one at a time with the procedures. The macro parameters BEGIN and END are useful in case there is a need to generate reports for just a number of schools. %REPORTS(3,3) will generate the reports for just the 3rd school. %REPORTS(14,25) will generate reports for the 14th through 25th schools. In the example, %INCLUDE is a chapter 12 feature. We should probably have a more sophisticated method to handle indexing of schools.

6 Another Solution %MACRO REPORTS (BEGIN, END); %DO INDEX=&BEGIN %TO &END; OPTIONS PAGENO=1; PROC PRINT DATA=MASTER; BY SCHOOL; VAR SSN LAST FIRST MI EXEMPT; WHERE SCHOOL=&INDEX; TITLE ’Student Names and Exemption Status’; PROC MEANS DATA=MASTER; VAR GPA; TITLE ’Average GPA’; PROC FREQ DATA=MASTER; TABLES STANINE; TITLE ’Distribution of Reading Test Stanines’; %END; %MEND REPORTS; %REPORTS (1,50) The WHERE statement was used so that the only data set needed for the procedures is MASTER. In this example, we avoid creating WORK.SCHOOL1, WORK.SCHOOL2, etc.


Download ppt "Using Macros to Solve the Collation Problem"

Similar presentations


Ads by Google