%macro genkeys(filetype=, /* usual meaning in oseda/mcdc data archive */ datadir=pub, /* set this to mscdc on the oseda version */ subdir=, /* input set in /&datadir/data/&filetype{/&subdir} */ setname=, /* name of input sas data set */ keyvars=, /* list of variables to generate values for */ keyquals=, /* list corresponding to keyvars containing names of variables whose values "qualify" the keys and are used to build arguments for format lookups. Use vertical bars as placeholders. A value of "| | state |" would mean use var state as qualifier for the 3rd keyvar. For example: keyvars=placefp,keyquals=state, ... keyvars=state county, keyquals=| state, ... */ keyfmats=, /* sas format name, without the "$" and ending ".", that can be used to assign value labels to the key variables. Use vertical bars ( | ) for keys that are not assigned using such a format. */ idvars=, /* list of sas variable names used as values for key variables. Can be left null if all values are assigned via formats. Not often used. Use vertical bars as placeholders. Example: keyvars=county,idvars=areaname -or- keyvars=msacmsa county,idvars=| areaname */ revdate=29Nov02, debug=0); %*--Coded and maintained by John Blodgett, Office of Social & Economic Data Analysis. Created under contract with the Missouri State Census Data Center.--*; %*--This macro takes parameters describing a set of "key" character type variables in a SAS data set within the /&datadir/data space, and generates an html page corresponding to the specified data set which contains the values, value labels and frequncy of occurrence of those values within the data set. Intended for use within the OSEDA/MCDC uexplore application.---*; %local b1; %*--used as pointer control to back up 1 space-; %let b1=%str(+(-1)); %local vbar; %let vbar=%quote(|); %libname(&filetype,&subdir,libref=insas); %local setin; %let setin=insas.&setname; %local subdir2; %if %quote(&subdir) ne %str() %then %let subdir2=%str(/)&subdir; %else %let subdir2=&subdir; %local i; %let i=1; %local idvarsed; %let idvarsed=; %do %until(&key eq %str()); %let key=%scan(&keyvars,&i); %if &key ne %str() %then %do; %let key&i=&key; %let qual&i=%scan(&keyquals,&i,%str( )); %let fmat&i=%scan(&keyfmats,&i,%str( )); %let idvar&i=%scan(&idvars,&i,%str( )); %let idvar=&&idvar&i; %if %quote(&idvar) ne &vbar and %quote(&idvar) ne %str() %then %let idvarsed=&idvarsed &idvar; %if &debug %then %put _local_ ; %end; %let i=%eval(&i + 1); %let key=%scan(&keyvars,&i); %end; %let nkeyvars=%eval(&i -1); %let keyn=&&key&nkeyvars; %*--we write the web page in same directory and with filename corresponding to the SAS data set file. We just remove the "sdd01" and substitute the "Keyvals.html"---; filename htmlout "/&datadir/data/&filetype&subdir2/&setname..Keyvals.html"; %if &debug %then %put _all_; data _values; set &setin (keep=&keyvars &idvarsed ) end=_last; drop &keyvars &idvarsed; length _keyno 3 _keyvar $32 _value $16 _vallab $64 _keyqual $32 _keyarg $16; drop _keyarg; if _n_=1 then do; file htmlout; put "
"/ "Key Variable Values for &filetype..&setname "; put "

" "Key Variable Values for &filetype..&setname

"; put "Variables: " ; %do i=1 %to &nkeyvars; %let key=&&key&i; if i > 1 then put ' .| .'; put "&key "; %end; put "
"; end; *--we number the keys using _keyno to allow us to specify an order for the keys. The most important ones should go first, not the one first in the alphabet--; _keyno=0; %do i=1 %to &nkeyvars; %if %quote(&&idvar&i) eq &vbar %then %let idvar&i=; %if %quote(&&fmat&i) eq &vbar %then %let fmat&i=; %if %quote(&&qual&i) eq &vbar %then %let qual&i=; %let key=%trim(&&key&i); %let fmat=%trim(&&fmat&i); %let qual=%trim(&&qual&i); _keyno+1; _keyvar="&key"; _value=&key; %if %quote(&&idvar&i) ne %str() %then %do; _vallab=&&idvar&i; %end; %else %if %quote(&fmat) ne %str() %then %do; %if %quote(&qual) ne %str() %then %do; %if &qual=cnty %then %do; if cnty=" " then cnty=substr(county,3,3); *<===; %end; _keyqual=&qual; _keyarg=trim(_keyqual)||&key; _vallab=put(_keyarg,$&fmat..); %end; %else %do; _vallab=put(&key,$&fmat..); %end; %if &fmat eq slvl %then %str( _vallab=substr(_vallab,5); ); %end; %if &debug %then %put _local_; link out; %end; return; out: if _value ne " " then output; return; run; proc sort out=sortout; by _keyno _keyqual _value ; run; data _null_; set sortout end=lastval; by _keyno _keyqual _value; file htmlout mod; if first._keyno then do; put "

Values for variable " _keyvar "

"/ ''; n_values=0; ncol=0; put ""; end; if first._value then do; n_values+1; nthisval=0; end; nthisval+1; if last._value then do; ncol+1; if ncol=6 then do; ncol=1; put "" /""@; end; put ''; end; if last._keyno then do; put "" / "
' @; if _keyqual ne " " then put "(" _keyqual ")" @; put "" _value &b1 "=" _vallab @; if nthisval gt 1 then put "(N=" nthisval &b1 ")" @; put '
"; end; if lastval then do; put /""; end; run; %mend genkeys;