SAS PROGRAMMING HANDOUT #21 This handout writes a SAS macro to check for creating GROUPs of data based on pre-determined equal spaced cutpoints. OPTIONS MACROGEN; *this will generate output helpful in finding errors; %MACRO GROUP1(DATA1=,DATA0=,VAR=,LOW=,HIGH=,WIDTH=); %************************************************************************* Macro parameters: DATA1 - name of output data set DATA0 - name of input data set VAR - variable to be made into groups LOW - left endpoint of lowest interval HIGH - right endpoint of highest interval WIDTH - width of intervals %*************************************************************************; DATA &DATA1; SET &DATA0; NN=1; GROUP=1; %DO N=&LOW %TO &HIGH %BY &WIDTH; %*the values in the %DO loop should be integers; IF &LOW <= &VAR < %EVAL(&LOW+&WIDTH) THEN GROUP=NN; NN+1; %LET LOW=%EVAL(&LOW+&WIDTH); %END; DROP NN; PROC PRINT; ************************************************; %MEND; *a macro can be saved on a diskette, for example; * as A:\GROUP.SAS ; *and then invoked with the command: ; * %INCLUDE 'A:\GROUP.SAS'; ; ************************************************; DATA JIM; INPUT X @@; CARDS; 100 45 52 45 49 3 12 34 120 4 89 93 36 85 48 92 18 9 %GROUP1(DATA1=OUT,DATA0=JIM,VAR=X,LOW=0,HIGH=150,WIDTH=30); *the values LOW, HIGH, and WIDTH must be integers; PROC PRINT DATA=OUT; PROC FREQ DATA=OUT; TABLES GROUP; RUN; ================================================================================================ This is very similar to the above program except that it computes the LOW and HIGH values using the MIN and MAX of the data. OPTIONS MACROGEN; *this will generate output helpful in finding errors; %MACRO GROUP2(DATA1=,DATA0=,VAR=,WIDTH=); %************************************************************************* Macro parameters: DATA1 - name of output data set DATA0 - name of input data set VAR - variable to be made into groups WIDTH - width of intervals %*************************************************************************; DATA &DATA1; SET &DATA0; PROC MEANS NOPRINT; VAR &VAR; OUTPUT OUT=AA MIN=LL MAX=HH; %*the values in the %DO loop should be integers; DATA AA; SET AA; LL=INT(LL); HH=INT(HH); CALL SYMPUT('LOW',LL); CALL SYMPUT('HIGH',HH); DATA &DATA1; SET &DATA1; NN=1; GROUP=1; %DO N=&LOW %TO &HIGH %BY &WIDTH; IF &LOW <= &VAR < %EVAL(&LOW+&WIDTH) THEN GROUP=NN; NN+1; %LET LOW=%EVAL(&LOW+&WIDTH); %END; DROP NN; PROC PRINT; %MEND; DATA JIM; INPUT X @@; CARDS; 100 45 52 45 49 3.2 12 34 120.4 4 89 93 36 85 48 92 18 9 %GROUP2(DATA1=OUT,DATA0=JIM,VAR=X,WIDTH=30); PROC PRINT DATA=OUT; PROC FREQ DATA=OUT; TABLES GROUP; RUN; ====================================================================================================== This handout writes a SAS macro to check for creating GROUPs of data based on pre-determined equal spaced cutpoints, without the restriction of integer values. OPTIONS MACROGEN MPRINT SYMBOLGEN; TITLE; FOOTNOTE; %MACRO GROUP3(DATA1=,DATA0=,VAR=,LOW=,HIGH=,WIDTH=); DATA &DATA1; SET &DATA0; NN=1; GROUP=1; %LET DD=%SYSEVALF((&HIGH-&LOW)/&WIDTH); %LET AA=%SCAN(&DD,1,'.'); %LET BB=%EVAL(&AA+1); %DO N=1 %TO &BB %BY 1; IF &LOW <= &VAR < %SYSEVALF(&LOW+&WIDTH) THEN GROUP=NN; NN+1; %LET LOW=%SYSEVALF(&LOW+&WIDTH); %END; DROP NN; PROC PRINT; %MEND; DATA JIM; INPUT X @@; CARDS; 100 45 52 45 49 3 12 34 120 4 89 93 %GROUP3(DATA1=OUT,DATA0=JIM,VAR=X,LOW=0.1,HIGH=150.3,WIDTH=25.5); PROC PRINT; PROC FREQ DATA=OUT; TABLES GROUP; RUN;