/******************************************************************** * NAME: genagg PGMR: BLODGETT * * FUNCTION: Generate sas code to invoke agg macro for a specified * * dataset using a Variables metadata set * * * * NOTES: See /pub/data/Tools/genaggs.sas for invocations. * ********************************************************************/ %GLOBAL REVDATE; %LET REVDATE=12/19/2002 11:17AM; *<--DATE OF LAST REVISION; /********************************************************************/ /* BEGIN MACRO DEFINITION */ /********************************************************************/ %MACRO genagg ( libref=, /* REQUIRED. This libref must be defined when the macro is invoked. The macro will obtain the physical path associated with the libref and will generate the output code file in the Tools/agg&libref..sas file by default. Use the standard mnemonic for the "filetype". */ metaset=&libref..Variables, /* SAS dataset containing the metadata. Must have 1 obs per variable with special varnames name, typelen, WtVar. */ sascode=, /* this can be a fileref. If left blank, the program will figure it out by getting the path value for &libref and then writing to the file &libpath/Tools/agg&libref..sas */ setin=,setout=,agglvl=1,aggby=, grand=0,dropvars=,facvar=, /* just place holders usually but if values are entered they are passed as default values in the generated code */ debug=0); /* END */ %PUT %STR( ); %PUT ***************************************************************; %put * genagg macro Rev &revdate begin execution *; %put * Missouri State Census Data Center *; %PUT ***************************************************************; %PUT %STR( ); %if &libref=%str() %then %do; %put ****Required parm libref not specified...macro aborting****; %goto endmac; %end; %if (%sysfunc(libref(&libref))) %then %do; %put %sysfunc(sysmsg()); %put ****Value of parm libref does not referecne a valid libref...macro aborting****; %goto endmac; %end; %local libpath; %let libpath=%sysfunc( pathname(&libref)); %if &debug %then %put libpath= &libpath; %if &sascode eq %str() %then %do; filename aggcode "&libpath./Tools/agg&libref..sas"; %let sascode=aggcode; %end; *---Begin data step to read the metadata and generate the partial agg invocation code---; data _null_; file &sascode ; put '%agg' "(setin=&setin ," / " setout=&setout ," / " aggby=&aggby ," / " agglvl=&agglvl ," / " vars=ALL," / " facvar=&facvar ," / " dropvars=&dropvars," / " grand=&grand , " ; length _means _meanwts $32000; *---Following loop reads the metadata (---.Variables) set and stores summary variables _means , _meanwts, _naggvars, _nmeans -- to be used in generating agg parm code below-; do until (_lastobs); set &metaset (keep=Name TypeLen WtVar PctFlag) end=_lastobs; where TypeLen=:'N'; _naggvars+1; if wtvar ne ' ' then do; _means=trim(_means)||' '||Name; _meanwts=trim(_meanwts)||' '||WtVar; _nmeans+1; %if &debug %then %do; file print; title2 'List of Means Variables with Corresponding Weights'; put _nmeans 3. +2 Name $char32. WtVar $char32. ; if mod(_nmeans,8)=7 then put; file &sascode; %end; if Mod(_nmeans,8)=7 then do; _meanwts=trim(_meanwts)|| '09'x; _means=trim(_means)||'09'x; *<---tab characters used to denote where we want to insert line breaks when we write code, after every 8 variables.---; end; end; end; *----Read .Variables loop----; _meanwts=trim(left(_meanwts))||'09'x; _means=trim(left(_means))||'09'x; *<--add delimiters at end to simplify parsing logic--; put ' naggvars=' _naggvars ','; length _meanline _meanwtline $200; if _nmeans then do; put ' nmeans=' _nmeans ',' / ' nmwts=' _nmeans ',' / ' means=' ; _meanline=scan(_means,1,'09'x); _nchunk=1; do while(_meanline ne ' '); put +3 _meanline ; _nchunk+1; _meanline=scan(_means,_nchunk,'09'x); end; put +3 ',meanwts='; _meanwtline=scan(_meanwts,1,'09'x); _nchunk=1; do while(_meanwtline ne ' '); put +3 _meanwtline ; _nchunk+1; _meanwtline=scan(_meanwts,_nchunk,'09'x); end; end; *--if _nmeans do group--; put ' ,debug=0)' ; stop; run; %endmac: %MEND genagg;