Date: Tue, 6 Aug 1996 09:11:41 EDT
Reply-To: whitloi1@WESTATPO.WESTAT.COM
Sender: "SAS(r) Discussion" <SAS-L@UGA.CC.UGA.EDU>
From: Ian Whitlock <whitloi1@WESTATPO.WESTAT.COM>
Subject: Re: Q: a macro question
Subject: Q: a macro question
Summary: Use macaro variables to remove repetitive decisions.
Respondent: Ian Whitlock <whitloi1@westat.com>
Sung-Il Cho <sungil@HOHP.HARVARD.EDU> asks how to eliminate the
repetitive nature of the %IFs in his macro
> %macro summary(data=, expo=, disease=, stratum=, freq=,
> e_code=hi, d_code=low);
>
> proc sort data=&data; by &stratum
> %if %upcase(%substr(&d_code,1,2))= HI %then %str(descending);
> &disease
> %if %upcase(%substr(&e_code,1,2))= HI %then %str(descending);
> &expo ;
> proc means n noprint data=&data;
> var &disease; by &stratum
> %if %upcase(%substr(&d_code,1,2))= HI %then %str(descending);
> &disease
> %if %upcase(%substr(&e_code,1,2))= HI %then %str(descending);
> &expo ;
> %if %quote(&freq) ne %then %str( freq &freq; );
> output out=temp(drop=_type_ _freq_) n=count;
> run;
> proc print; run;
> %mend summary;
The repetition comes from making the same decision more than once. To
eliminate it hold the decision in a macro variable and then refer to
the macro variable in multiple places.
%macro summary(data=, stratum=, freq=,
e_code=hi, expo=, d_code=low, disease= );
%local emod dmod ;
%if %upcase(%substr(&d_code,1,2))= HI %then
%let dmod = descending ;
%if %upcase(%substr(&e_code,1,2))= HI %then
%let emod = descending ;
proc sort data=&data;
by &stratum &dmod &disease &emod &expo ;
run ;
proc means n noprint data=&data;
var &disease;
by &stratum &dmod &disease &emod &expo ;
%if %quote(&freq) ne %then %str( freq &freq; );
output out=temp(drop=_type_ _freq_) n=count;
run;
proc print data = temp ;
run;
%mend summary;
I would consider modifying the parameter structure to:
%summary(data=lib1.chd1, stratum=stratum, freq=freq,
expo=descending expo, disease=disease )
This form has fewer parameters for the user to remember. Now, of
course, the macro code gets longer and more complex, but it might be
easier to use.
Ian Whitlock <whitloi1@westat.com>
|