/* Update history: 2-8-16: Adding code to use a custom formats catalog associated with the input directory. We edit the fmtearch sas option accordingly. This module is invoked by the wgenkey.sas module in /pub/applications/utils as a SAS/intrnet app. */ %global allmissing; %include '/pub/sas/macros/streamfile.sas'; %macro wgenkey; %global kv ki kq kf libname memname filetype sd ow ft; %if &ft ne %str() %then %let filetype=&ft; %if %superq(filetype) eq %str() %then %let filetype=&libname; *-- Build name of output html file in the /SAS_temp directory. ; %local htmlfile; %if %superq(filetype) ne %superq(libname) %then %let htmlfile=%str(/SAS_temp/keyvals/&filetype..&libname..&memname..&kv.vals.html); %else %let htmlfile=%str(/SAS_temp/keyvals/&libname..&memname..&kv.vals.html); *-- ow is an overwrite flag indicating to rewrite the file even if it exists. ; %if %nrbquote(&ow)=%str() %then %let ow=0; %if &ow eq 0 and %sysfunc(fileexist(&htmlfile)) %then %goto copyback; %local b1; *-- Used as pointer control to back up 1 space. ; %let b1=%str(+(-1)); %local vbar; %let vbar=%quote(|); filename htmlfile "&htmlfile"; libname &libname "/pub/data/&filetype/&sd" access=readonly; *-- Code added 9/03 and 10/08 to detect special cases where we need to load some temporary value-label formats. ; %if %nrbquote(&filetype)=popests and %index(&memname,casrh) %then %do; %include '/pub/data/popests/Tools/casrh_formats.sas'/nosource2; %end; %else %if %quote(&libname)=saipe and %index(&memname,sahie) %then %do; options fmtsearch = (saipe library work); %end; %else %do; *-- Generalize ability to access permanent SAS format codes unique to the filetype (added 2-8-16). ; %global existsfc; %let existsfc=0; data _null_; _existsfc = fileexist("/pub/data/&filetype/&sd"||"/formats.sas7bcat"); call symput('existsfc',put(_existsfc,1.)); stop; run; %if &existsfc %then %do; options fmtsearch=(&libname sasctlgs mable14 work); %end; %end; *-- If we get here the file does not exist or we want to rewrite it. ; %if %quote(&ki) eq &vbar %then %let ki=; %if %quote(&kf) eq &vbar %then %let kf=; %if %quote(&kq) eq &vbar %then %let kq=; %let key=%trim(&kv); %let fmat=%trim(&kf); %let qual=%trim(&kq); data _null_; file htmlfile; put "Frequencies for Selected Variable"; put '
'; %streamfile('/pub/includes/app_header.html', htmlfile, mod); run; data _values; set &libname..&memname (keep=&kv &ki &kq ) end=_last; %if %superq(sorted) ne %str() %then %let notsorted=; %else %let notsorted=notsorted; by %if %superq(kq) ne %str() %then %do; &kq ¬sorted %end; %if %superq(kv) ne %str() %then %do; &kv ¬sorted %end; ; length _value $64 _vallab $200; *** _keyqual $32 _keyarg $16; drop _keyarg; if _n_=1 then do; file htmlfile mod; put "

Values with frequencies for variable &kv in dataset &libname..&memname

"; end; _value=&key; if last.&kv then do; %if %quote(&ki) ne %str() %then %do; _vallab=&ki; %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; end; link out; return; out: __freq__+1; *-- We count consecutive duplicate values to keep size of dataset down. ; if last.&kv then do; do; output; __nout+1; end; __freq__=0; end; if _last and __nout le 0 then do; file htmlfile; put '

Note: All values of this variable on this dataset are missing.

' / ''; call symput('allmissing','1'); end; return; keep &qual &key _vallab __freq__; run; %put *** allmissing= &allmissing ***; %if %superq(allmissing)=1 %then %goto copyback; proc sort out=sortout(compress=no); by &qual &key _vallab ; run; proc sql; create table _vals as select distinct %if &qual ne %then %str(&qual ,); &key from sortout; data _null_; set sortout end=lastval ; by &qual &key _vallab ; file htmlfile mod; if _n_=1 then do; if 0 then set _vals nobs=_nobs; retain _:; put '
'; end; if first.&key then nthisval=0; nthisval+ __freq__; format &key ; *-- We do NOT want it to print with a format. ; if last.&key then do; put '

'; %put &qual; %if &qual ne %str() %then %do; format &qual; put "(" &qual &b1 ")" @; %end; put "" &key &b1@; if _vallab ne " " then put "=" _vallab @; else put " " @; if nthisval gt 1 then put "(N=" nthisval &b1 ")" @; put '

'; end; if lastval then do; *--close out the table--; put '
'; end; run; quit; data _null_; file htmlfile mod; %streamfile('/pub/includes/footer.html', htmlfile, mod); run; %copyback: data _null_; file _webout; infile "&htmlfile"; input; put _infile_; run; %mend wgenkey;