%macro genkeys(filetype=, /* usual meaning in oseda/mcdc data archive */ libref=, /* name used for input data library. Gets assigned default value of &filetype */ 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 */ htmlout=, /* Normally the macro will figure out where to write the output html file. But this parm allows user to specify it. */ revdate=3/29/2003 1:27PM, 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(|); %include '/pub/sas/macros/libname.sas'; %if &libref eq %str() %then %let libref=&filetype; %libname(&filetype,&subdir,libref=&libref); libname sascats '/pub/sas/catalogs' access=readonly; options fmtsearch=(sascats work); %local setin; %let setin=&libref..&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=; %local qualvarsed; %let qualvarsed=; %local _idvar _qualvar; %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; %let _qualvar=&&qual&i; %if %quote(&_idvar) ne &vbar and %quote(&_idvar) ne %str() %then %let idvarsed=&idvarsed &_idvar; %if %quote(&_qualvar) ne &vbar and %quote(&_qualvar) ne %str() %then %let qualvarsed=&qualvarsed &_qualvar; %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 "sas7bdat" and substitute the "Keyvals.html"---; %if %superq(htmlout) ne %str() %then %do; filename htmlout "&htmlout"; %end; %else %do; filename htmlout "/&datadir/data/&filetype&subdir2/&setname..Keyvals.html"; %end; %if &debug %then %put _all_; data _values(compress=yes); set &setin (keep=&keyvars &idvarsed &qualvarsed ) end=_last; drop &keyvars &idvarsed; length _keyno 3 _keyvar $32 _value $64 _vallab $200 _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

"; %if &nkeyvars gt 1 %then %do; put "Variables: " ; %do i=1 %to &nkeyvars; %let key=&&key&i; if &i > 1 then put ' .| .'; put "&key "; %end; put ""; %end; 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(compress=yes); by _keyno _keyqual _value ; run; *---New code Jan 2003 to write the table in column instead of row order---; proc sql noprint; create table ucodes as select distinct _keyno, _keyqual, _value from sortout; quit; *---Idea here is to count the number of distinct cases for each key variable---; data _null_; set ucodes end =_last; by _keyno; if first._keyno then n=1; else n+1; if last._keyno then do; retain nvar 'nvxxx'; substr(nvar,3)=left(put(_keyno,3.)); call symput(nvar,left(put(n,6.))); end; run; data _null_; set sortout end=lastval; by _keyno _keyqual _value; file htmlout mod; array _valcounts(&nkeyvars) _temporary_ ( %do i=1 %to &nkeyvars; &&nv&i %end; ); if first._keyno then do; _nvalues=_valcounts{_keyno}; _npercol=int((_nvalues+4)/5); _ncols=1; retain _:; put "

Values for variable " _keyvar "

"/ ''; n_values=0; ncol=0; put ""; end; if first._value then do; n_values+1; if n_values gt _npercol then n_values=1; nthisval=0; end; nthisval+1; if last._value then do; if n_values=1 then do; *--first value for this variable, so we are starting a new table-; if _ncols > 1 then put ''; _ncols+1; put '" / "
'; else put '
' ; end; else put '

'; if _keyqual ne " " then put "(" _keyqual ")" @; put "" _value &b1@; if _vallab ne " " then put "=" _vallab @; else put " " @; if nthisval gt 1 then put "(N=" nthisval &b1 ")" @; end; if last._keyno then do; *--close out the table--; put "

"; end; if lastval then do; put /""; end; run; %mend genkeys;