%macro dovart(name,label,val,fmt=,len=,comment=,wtvar=,kw=,dup=0); %let revdate=8/19/2004 12:49PM; %*--This utility macro has 2 modes: meta and data (specified by the global parm dvmode). In meta mode it generates values for the variables in a metadata set (name, table, tableno, varno, label, etc.) In data mode it will create the variable &name with label &label and format code &fmt (if not null). If val parm is coded then it will be used as the expression on right side to assign a value to &name. Otherwise the assignment statement &name = _val will be generated. So the invoking code should assign the value to the temp variable _val prior to invoking this macro. Global parm pctof will be checked for non-null value and if there is one will presume it to be the name of a variable that can be used as the denominator in a percentage calculation. The variable Pct&name will be created with value 100*&name/&pctof . It will have a length of 4 and format code of 5.1 and a a label of "% &label" . On the meta dataset the flag variable PctFlag will be set to 1 for these derived percentage vars. Originally was named dovarsf3 and that still works as an alias. But there is nothing about this macro that makes it specific to any one filetype so we renamed it (11-12-02) to be generic. Adding lvl global parm: 8-22-02 Adding display global parm: 8-23-02 Assigning numeric rather than character values to display and lvl vars, 10-22-02. Adding VarNo 11-12-02 . Adding dup parm 12-14-02. Will create new dup flag var to indicate duplicate variables. These are vars that are to be displayed as part of more than 1 table. But on the actual datasets there is still just the one variable so for many uses of the Variables metadata set we shall need to filter out dup=1 observations. Adding check for the dup=1 spec so that in data mode we do not generate duplicate code. 07-16-03. --*; %local null; %let null=%str(); %global curtable curtabno; %if &curtabno = &null %then %let curtabno=0; %if %superq(label) eq &null %then %let label=&name; %else %if %qsubstr(&label,1,1)=%str(%') %then %do; %*--if label enclosed in single quotes remove them---; %let label=%substr(&label,2,%eval(%length(&label)-2)); %end; %*--default format code is comma9. if none is specified-; %*--unless we detect that its a variable with label ending with word "Income", in which case it defaults to dollar9.-; %if %quote( %sysfunc(scan(&label,-1,'"-()% '))) = %str(Income) and %quote(&fmt) eq &null %then %let fmt=%str(dollar9.); %if %quote(&fmt) eq &null %then %let fmt=%str(comma9.); %if %length(&name) gt 16 %then %put *****Warning: Variable name is longer than 16 characters: &name ****; %*---adding the test for dup=0 here, 7-03, jgb-----; %if &dvmode = %str(data) and &dup=0 %then %do; %*--Build an attrib statement for the variable which may specify label, length and format-; attrib &name label ="&label" %if %quote(&len) ne &null %then %str( length = &len ); %if %quote(&fmt) ne %str(none) %then %str( format=&fmt ); ; %*--end attrib stmt--; %*--Generate the assignment statement to define the value of the variable--; %*--if the value of &val ends with a slash and a single word we assume that it is division by the variable with name after the slash. In this case we qualify the assignment so that it does not take place when the denominator is 0 or missing.--; %local _denom _num _valedit; %let _valedit=%sysfunc(compress(&val,%str(()))); %*--remove parentheses for this edited version-; %let _numer=%scan(&_valedit,-2,%str(/)); %if %superq(_numer) ne %str() %then %let _denom=%scan(&_valedit,-1,%str(/)); %else %let _denom=; %if %superq(_denom) ne %str() %then %do; if abs(&_denom) > 0 then %end; &name = &val ; %*--And now generate the corresponding Pct variable if PctOf parm has a value--; %if &pctof ne &null %then %do; attrib Pct&name length=4 format=5.1 label="% &label"; if &pctof gt 0 then Pct&name=100*&name/&pctof; else Pct&name=.; %end; %end; %*---data gen mode---; %if &dvmode=%str(meta) %then %do; %if %bquote(&table) ne %bquote(&curtable) %then %do; %let curtabno= %eval(&curtabno + 1); %let curtable=&table; %let lvl=0; %end; Name="&name"; %if &lvl %then %do; %*--the lvl parm indicates an indentation level--; Label=repeat('09'x,%eval(&lvl-1))||"&label"; %end; %else %do; Label="&label"; %end; Definition="&val"; Comment="&comment"; Format="&fmt"; Table="&curtabno.. &table"; TableNo=&curtabno; VarNo+1; TypeLen="N&len"; Universe="&universe"; UnivVar="&pctof"; WtVar="&wtvar"; Lvl=&lvl; %if &display eq %str() %then %let display=0; Display=&display; PctFlag = (Name=:'Pct'); Keywords = "&kw"; Dup= &dup; RefTables="&reftables"; output; %if &pctof ne &null %then %do; Name="Pct&name"; Label="% &label"; Definition="100*&name / &pctof"; Comment=" "; Format="5.1"; TypeLen="N4"; WtVar="&pctof"; PctFlag=1; Keywords="Pct &pctof &kw"; output; %end; %end; %*---meta mode processing---; %mend dovar;