%macro dovar(name,label,val,fmt=,len=,comment=,wtvar=,kw=,chkval=,moe=,dup=0); %let revdate=6/28/2011 8:14:30 AM; %*--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. Adding chkval parm to do value-checking in data mode. Adding moe parm to allow genrating a corresponding moe variable ("margin of error" measure, as used in acs datasets.) Modifying how moe parm is checked to allow for moe to be a formula for the value instead of just a 0/1 option. Modifying so that UnivVar for Pct vars is set to blank. (8-18-08) Undoing this 9-24-08 (!!) Checking for moe=#base spec and using it to define the moe variables using Bureau-provide formulae and assuming input is acs basetbls data. Syntax errors discovered in unfinished code to process moe variables, near where it says "To be continued". jgb. 6-28-11 --*; %local null; %let null=%str(); %global curtable curtabno; %if &curtabno = &null %then %let curtabno=0; %let _ticstring=' '; %if %superq(label) eq &null %then %let label=&name; %else %if %qsubstr(&label,1,1)=# %then %do; %*-changed right side from %str(%) 6-27-11 (!!!!!!)-; %*--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.); %*--we use the global var moe_default to assign a default value to moe. The "default default" value is 0, no moe vars generated--; %global moe_default; %if &moe_default=%str() %then %let moe_default=0; %if %superq(moe)=%str() %then %let moe=&moe_default; %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; %*--If moe flag is set then we attempt to assign it a value provided the defn indicates we are copying a single table cell value so we can copy the corresponding Marg value-; %if %superq(moe)=1 and %scan(%superq(val),2)=%str() %then %do; attrib &name._MOE length=4 label='+/-'; &name._MOE=Marg&val; drop Marg&val; %end; %else %if %superq(moe) ne 1 and %superq(moe) ne %str() and %superq(moe) ne %str(0) and %superq(moe) ne %str(#base) %then %do; %*---moe actually specifies the formula--; &name._MOE=&moe; %end; %else %if %superq(moe)=%str(#base) %then %do; %*---We presume we are accessing the basetbls data, such as that stored in /pub/dat/acs2009/basetbls data sets (in a data step where we merge the corresponding estimates and moe data setrs).---; %let _ix=%index(%str(&val),%str(-)); %*--looking for a variable interval, signaled by 2 variables connected by a dash--; %if &_ix %then %if %index(%str(123456789),%substr(&val,%eval(&_ix-1))) %then %do; %*--iff a digit precedes the minus sign--; %let _nvarsvalx=0; %let _valx=%xpndlst(&val,_nvarsvalx); array array_&name (&_nvarsvalx) &_valx ; %*<=======================To Be Continued====???=========================; %end; %else %if %index(&val,%str(+)) %then %do; %let _nvarsvalx=0; %let _valxedited=%sysfunc(compress(&val,%str(+))); %let _valx=%xpndlst(&val ); %*---This was found without a closing right paren 6-28-11 (!!??)--; %end; %end; %*---#base processing---; %*---if a check value specified verify that we got the value right----; %if &chkval ne %str() %then %do; if _n_=1 then do; file print; _value_found=round(&name,.1); if _value_found ne round(&chkval,.1) then do; _nbadchkvals+1; ***if _nbadchkvals=1 then put '****Values Not Matching chkval Parms***** ' /; put &name.= +2 "chkval=&chkval" "*****NON-MATCH******"; end; else put &name.= " (match)"; end; %end; %*---chkval processing---; %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"; UnivVar="&name"; WtVar="&pctof"; PctFlag=1; Keywords="Pct &pctof &kw"; output; %end; %end; %*---meta mode processing---; %mend dovar;