/******************************************************************** * NAME: genvars SYSTEM: PGMR: BLODGETT * * FUNCTION: Generate std set of key indicator measures. We specify* * a "base" variable name and the macro generates a series of * * related time-based vars as well as pct vars and change/pctchange* * NOTES: Used primarily for creating datasets in the indctrs db. * ********************************************************************/ %GLOBAL REVDATE; %LET REVDATE=30May02; *<----DATE OF LAST REVISION; /********************************************************************/ /* BEGIN MACRO DEFINITION */ /********************************************************************/ %MACRO genvars( basevars, /* these are the root name of the generated variables, as well as (by default) the names on input sets */ namesin=&basevars, /* default is that input names are same as output base names. E.g. input contains totpop, tothhs, families and gqpop. On output these are called totpop_2000 totpop_1990 tothhs_2000 tothhs_1990, etc. */ years=1990 2000, pctsof=, /* variables to use to determine calculated pct_ variables */ pctof=, /* alias for pctsof */ pct=1, /* says to calculate Pct-- vars: specify pct=0 to suppress */ chg=1, /* says to calculate Chg-- vars: specify chg=0 to suppress */ pctchg=1, /* says to calculate PctChg-- vars: specify pctchg=0 to suppress */ attribs=1, /* says to generate length, format and label statements for vars. New 5-30-02 */ debug=0); /* END */ %PUT %STR( ); %PUT ***************************************************************; %put * genvars macro Rev &revdate begin execution *; %PUT ***************************************************************; %PUT %STR( ); %*--Modified 5-29-02 to create the global variables _pcts and _pctsof with complete lists of all percentages calculated and what denominators were used. These can be used in post processing -- specifically when doing an aggregation these vars can be plugged in for means and meanwts parms values. ---*; %global _pcts _pctsof; %let _pcts=; %let _pctsof=; %local i j nvars nyears npctsof err namein basevar y1 y2 keep pcts denom; %let nvars=%lenlst (&basevars); %if &nvars=0 %then %do; %let err=No basevars parm specified; %goto flagerr; %end; %let nyears=%lenlst(&years); %if &nyears ne 2 %then %do; %let err=%str(Invalid years parm. Must specify exactly 2 values.); %goto flagerr; %end; %if %quote(&pctsof) ne %then %let pctof=&pctsof; %if %lenlst(&namesin) ne &nvars %then %do; %let err=Length of namesin parm does not match length of basevars parm; %goto flagerr; %end; %let y1=%scan(&years,1); %let y2=%scan(&years,2); %let sy1=_&y1; %let sy2=_&y2; %*--var suffixes include an underscore prior to the year--; %let npctsof=%lenlst(&pctsof); %if &npctsof=0 or &npctsof=1 or &npctsof=&nvars %then ; %else %do; %let err=pctsof parm does not match up with basevars; %goto flagerr; %end; %let keep= ; %let pcts=; %do i=1 %to &nvars; %let namein=%scan(&namesin,&i); %let basevar=%scan(&basevars,&i); %let keep=&keep &basevar&sy1 &basevar&sy2; %if &npctsof %then %do; %if &npctsof=1 %then %let denom=&pctof; %else %let denom=%scan(&pctsof,&i); %if &denom ne %str(_) %then %do; %*--Insert an underscore in pctsof list to skip a pct calculation-; if &denom&sy1 >0 then Pct&basevar&sy1 = 100 * &namein&sy1/&denom&sy1; else Pct&basevar&sy1=.; if &denom&sy2 >0 then Pct&basevar&sy2 = 100 * &namein&sy2/&denom&sy2; else Pct&basevar&sy2=.; %let keep= &keep Pct&basevar&sy1 Pct&basevar&sy2; %let pcts= &pcts Pct&basevar&sy1 Pct&basevar&sy2; %let _pctsof= &_pctsof &denom&sy1 &denom&sy2; %if &debug %then %put pcts= &pcts; %end; %end; %*--calcualate the change-over-time and Pct change over time variables---; Chg&basevar&sy1&sy2= &basevar&sy2 - &basevar&sy1; if &basevar&sy1 > 0 then PctChg&basevar&sy1&sy2= 100*Chg&basevar&sy1&sy2 / &basevar&sy1; %let keep=&keep Chg&basevar&sy1&sy2 PctChg&basevar&sy1&sy2; %let pcts=&pcts PctChg&basevar&sy1&sy2; %let _pctsof=&_pctsof &basevar&sy1; %if &attribs %then %do; length &pcts 4; format &pcts 6.1; %end; %end; keep &keep; %goto fini; %flagerr: %put ****genvars macro has detected a problem with parameter specs and will not be generating any code: ; %put ****: &err; %put _all_; this code being generated to cause the invoking data step to fail ********************** %fini: %let _pcts=&pcts; %if &debug %then %put _all_; %mend genvars;