%macro unistats(revdate=%str(12/15/2009 10:50:09 AM), /* NOTE!!!! Naming convention anomaly: Name of macro is not name of the sas file!!!! */ path=indctrs, /* path where data is located. If it starts with a / then we use it as-is. Otherwise we pre-append /pub/data/ to it. Expects to find a unistat_extrema subdirectory within the path where output report is written. Parm added 5-06 */ set=, /* name of the indctrs dataset to be processed */ libref=&path, /* library reference name used to reference the data */ indepth=0, /* set indepth=1 to indicate data is in the indepth subdir of indctrs */ byvars=, xvars=, /* numeric variables to exclude from analysis */ sumlev=, nozeros=0, /* nozeros=1 causes stats with no non-missing cases to be omitted from reports/datasets */ areas=Missouri Counties, /* For the report title */ debug=0); *--This macro will generate statistical summary reports on the variables in the dataset specified by path and set. There will be a univariate statistics report and an extrema report showing the 15 highest and lowest values.--; *---Edited Nov. 7, 2002 to make use of the new genlong macro. This allows us to avoid the SAS limitation of at most 4000 bytes per word.---; %local datalib; %let datalib=&libref; %if %qsubstr(&path,1,1)=%quote(/) %then ; %else %let path=/pub/data/&path; libname &datalib "&path" access=readonly; libname unistats "&path/unistat_extrema"; %*if &indepth %then %let datalib=indepth; %*else %let datalib=indctrs; title "Univariate Measures for Indicator Variables in &path Set &set"; options nodate ls=150; ods html file="&path/unistat_extrema/&set._unistats.html" style=sasweb; *<---direct html output---; proc contents data=&datalib..&set noprint out=_vars(keep=varnum type name); run; proc sort data=_vars(where=(type=1)); by varnum; proc sql noprint; select name into :vars separated by ' ' from _vars; quit; %put vars= &vars; data varvals; set &datalib..&set; %if &sumlev ne %str() %then %do; where SumLev="&sumlev"; %end; array _vars(*) &vars; length _varno 3 _value 8; keep _varno _value county &byvars; do _varno=1 to dim(_vars); _value=_vars(_varno); output; end; run; %if %quote(&byvars) ne %then %do; proc sort; by &byvars; run; %end; proc univariate data=varvals noprint; %if %quote(&byvars) ne %then %do; by &byvars; %end; class _varno; var _value; output out=unistats n=N sum=Sum min=Min max=Max median=Median mean=Mean std=Std normal=Normal p1=P1 p5=P5 p10=P10 q1=P25 q3=P75 p90=P90 p95=P95 p99=P99; run; *---Post process to add name of indicator variable---; data unistats.&set; length indicator $32; set unistats; %if &nozeros %then %do; where n > 0; footnote 'Note: Cases where N=0 have been omitted'; %end; *---Mod, Nov, 2002 to replace above assignment with new code that gets around 400-byte-word limit in SAS-; %genlong(vars) ; drop _vars; indicator=scan(_vars,_varno); if indicator=' ' then do; put //"****************************PROBLEM!!!!!! Indicator is not being assigned a value in unistats.&set . "/ _varno= / "Program is aborting*************** "; abort abend; end; ***drop _varno; run; ods listing close; proc print d data=unistats.&set; %if %quote(&byvars) ne %then %do; by &byvars; %end; id indicator; title2 'Part 1: Basic Univariate Statistics'; var n sum mean median min max std normal; run; proc print d data=unistats.&set; %if %quote(&byvars) ne %then %do; by &byvars; %end; id indicator; title2 'Part 2: Distribution Statistics'; var min p1 p5 p10 p25 median p75 p90 p95 p99 max normal; run; ods html close; *-------Part 2: Generate Report Showing Lowest and Highest Values for Each indicator---------------*; *---this code is Missouri-county specific in that it looks for rankings 1-15 and 101-115 knowing we have exactly 115 cases; proc sort data=varvals; by &byvars _varno _value; run; data unistats.&set._ext; length Rank 3 Indicator $32 LowCounty $5 LowValue 5 HighCounty $5 HighValue 5; merge varvals unistats.&set(in=instats); by &byvars _varno; if instats; array _lowid{15} $5 _temporary_; array _lowval{15} _temporary_; array _highid{15} $5 _temporary_; array _highval{15} _temporary_; if first._varno then _rank=1; else _rank+1; if _rank <= 15 then do; _lowid(_rank)=county; _lowval(_rank)=_value; end; else if _rank > 100 then do; _ix=116-_rank; _highid(_ix)=county; _highval(_ix)=_value; end; if not last._varno then return; *---We do a lot of processing when we get to the end of the variable group---*; if _rank ne 115 then do; file log; _nwarn+1; if _nwarn le 25 then put '***Warning: We have extraneous of missing values for indicator # ' _varno ' ******'; end; ****indicator = scan("&vars",_varno); *---Mod, Nov, 2002 to replace above assignment with new code that gets around 400-byte-word limit in SAS-; %genlong(vars) ; drop _vars; indicator=scan(_vars,_varno); do Rank=1 to 15; LowCounty=_lowid(Rank); LowValue=_lowval(Rank); HighCounty=_highid(Rank); HighValue=_highval(Rank); output; *<-----------Here is the output statement. 1 obs per the 15 rank categories per Indicator--; end; format LowCounty HighCounty $mocnty.; keep Rank Indicator LowCounty HighCounty LowValue HighValue &byvars; label Rank='Rank Among Missouri Counties, High or Low' Indicator='Variable Being Ranked (Indicator)' LowCounty='County That Had the (Rank) Lowest Value of Indictor' LowValue='The (Rank) Lowest Value of Indicator' HighCounty='County That Had the (Rank) Highest Value of Indictor' HighValue='The (Rank) Highest Value of Indicator'; run; proc sort data=unistats.&set._ext; by &byvars indicator; run; ods html file="&path/unistat_extrema/&set._extrema.html" style=sasweb; *<---direct html output---; proc print data=unistats.&set._ext uniform label; by &byvars Indicator ; id &byvars Indicator; var rank LowCounty LowValue HighCounty HighValue; format rank 3.; title2 "Extrema Report Showing 15 Lowest and Highest Ranking &areas for Each Key Indicator"; run; ods html close; ods listing; %mend unistats;