/* This macro will generate a proc print step that will display user-specified variables from a user-specifeid dataset sorted (ranked) by a user-specified variable. John Blodgett. Originally coded in early 90s, revised slightly in 2006 and in 2008. */ %global revdate; %let revdate=05/22/2008; *<----date of last revision; /********************************************************************/ /* begin macro definition */ /********************************************************************/ /* end */ %macro rankrpt( setin=_last_, /* specifies sas data set used as input for generating the ranking report. */ rankvar=, /* required parm identifies the variable used to determine the ranking, i.e. the variable by which the data will be ranked. */ rankby=, /* alias for rankvar */ order=d, /* option to specify a(scending) or d(escending) rank order. default is descending. */ n2list=, /* required parm specifies the number of observations to display in the report. */ n2rank=, /* alias for n2list */ idvars=, /* list of variables that uniquely id cases. Used as part of id variables list in report step. The entire id var list is &rankvar &idvars &auxids */ ids=, /* alias for idvars */ idvar=, /* alias for idvars */ geocodes=,/* alias for idvars */ geocode=, /* alias for idvars */ auxids=, /* optional list of additional case id variables. these variables will be part of the "id" vars list in the proc print step but will not be used for sorting/ranking. typically, will contain "areaname" or other variable to further identify the case. */ auxid=, /* alias for auxids */ auxgeo=, /* alias for auxgeo */ auxgeos=, /* alias for auxids */ othvars=, /* optional list of other variables to appear in report but that appear in no other var list (not id/by/rank vars*/ othrvars=, /* alias for othvars */ byvars=, /* can have multiple rankings within by groups if this parm is non-null. e.g. could do report of top 10 counties by state for some variable. input d-set does *not* have to be sorted by this variable. */ byvar=, /* alias for byvars */ univvar=, /* optional. names a variable that contains a value which measures the size of the universe for &rankvar. appears in report and can be used in conjunction with minuniv */ minuniv=, /* optional. ignored if univvar not specified. data will be filtered using "&univvar ge &minuniv" if specified. using these parms allows getting rid of cases where the rankvar is based on such a small universe that we do not want it to be eligible for the report. when univvar and minuniv are specified, the filtering will be noted in a program-generated title */ minmax=, /* value used to pre-filter data prior to ranking. if order is ascending then this value is assumed to be a maximum, otherwise a minimum. thus, if user specifies "rankvar= meanage, order=d, minmax=30", the macro will generate a filter to ignore cases where meanage is < 30. if not specified then no such filtering will occur. */ maxmin=, /* alias for minmax */ ntitles=2, /* number of title statements specified by user for the reports. macro will generate additional title state- ments. */ prntopts=, /* options clause to be inserted after proc print stmt that generates report. e.g. prntopts=label double */ debug=0); %put %str( ); %put ***************************************************************; %put * rankrpt macro rev &revdate begin execution *; %put * missouri census data center *; %put ***************************************************************; %put %str( ); %*-----check for and utilize aliases for parameters---*; %if &rankby ne %then %let rankvar=&rankby; %if &n2rank ne %then %let n2list=&n2rank; %local _errmsg; %let _errmsg=; %if %nrbquote(&rankvar)=%str() %then %let _errmsg=Required parm rankvar was not specified; %else %if %nrbquote(&n2list) =%str() %then %let _errmsg=Required parm n2list not specified; %if %superq(_errmsg) ne %str() %then %do; %put; %put *** &_errmsg **** ; %put *** Macro will generate no code. ***; %goto endmac; %end; %if &maxmin ne %then %let minmax=&maxmin; %if %nrbquote(&ids) ne %then %let idvars=&ids; %else %if %nrbquote(&geocodes) ne %then %let idvars=&geocodes; %else %if %nrbquote(&geocode) ne %then %let idvars=&geocode; %else %if %nrbquote(&idvar) ne %then %let idvars=&idvar; %if %nrbquote(&auxgeos) ne %then %let auxids=&auxgeos; %else %if %nrbquote(&auxgeo) ne %then %let auxids=&auxgeo; %else %if %nrbquote(&auxid) ne %then %let auxids=&auxid; %if %nrbquote(&byvar) ne %then %let byvars=&byvar; %if %nrbquote(&othrvars) ne %then %let othvars=&othrvars; %let order=%lowcase(&order); %local desc dword; %if &order=d or &order=desc or &order=descending %then %let desc=1; %else %let desc=0; %if &desc=1 %then %let dword=descending; %else %let dword=%str(); %local also; %*--used in where statements. set to value of "also" once a where statement has been generated to allow subsequent where stmts to become "where also" statements-; %local lastby nt; data _r1; set &setin; keep &byvars &idvars &auxids &othvars &univvar &rankvar ; %if %nrbquote(&univvar) ne and &minuniv ne %then %do; where &univvar ge &minuniv; %let also=also; %end; %if &minmax ne %then %do; %if &desc=1 %then %str(where &also &rankvar ge &minmax;); %else %str(where &also &rankvar le &minmax;); %end; run; %*--the sort is by the by-variables, then by the ranking variable and -if a universe var is specified - we sort by it (descending) so that ties will be listed in order by largest universe--; proc sort data=_r1 ; by &byvars &dword &rankvar %if &univvar ne %then %str( descending &univvar); ; run; data r2v/view=r2v; set _r1; %if &byvars ne %then %do; %local nby; %let nby=%lenlst(&byvars); %*<======; %let lastby=%scan(&byvars,&nby); by &byvars; if first.&lastby then Rank=0; %end; Rank+1; if Rank le &n2list then output; %if &byvars eq %then %do; if Rank=&n2list then stop; %end; run; proc print data=r2v &prntopts; %if &byvars ne %then %str( by &byvars;); id Rank &idvars &auxids; var &rankvar &univvar &othvars; %if &minuniv ne %then %do; %local nt; %let nt=%eval(&ntitles + 1); title&nt h=1 "Universe Filter Applied: &univvar >= &minuniv"; %end; run; %endmac: %mend rankrpt;