/******************* URBAN INSTITUTE MACRO LIBRARY *********************
: Compile_num_desc
Macro: Compiles numeric statistics (n, sum, mean, std, min, max) for all variables in a data set. Default is to put statistics in a data set called WORK._DESC_&ds_name.
Description
: Open code
Use
: Peter Tatian
Author
***********************************************************************/
Compile_num_desc(
%macro stats=n mean std min max, /* List of statistics */
ds_lib=work, /* Input data set library */
ds_name=, /* Input data set name */
var_list=_numeric_, /* List of variables to include */
out_pre=work._desc, /* Prefix for output data set lib & name */
var_pre=_desc /* Output variable prefix */
);
/*************************** USAGE NOTES *****************************
:
SAMPLE CALLCompile_num_desc(
%stats=n mean std min max,
ds_lib=,
ds_name=Test,
var_list=a b
)
calculates n, mean, std, min, and max of variablesin new data set work._desc_test.
a and b and saves them in output data set for each input variable.
one observation in output data set are _name_ (name of input var),
Variables
and _desc_n, _desc_mean, etc.in data set WORK._DESC_&ds_name.
Default is to put statistics in global macro var _compile_num_desc_out.
Output data set name is saved *********************************************************************/
/*************************** UPDATE NOTES ****************************
08/31/04 Peter A. Tatian
09/02/04 Added cleanup of temporary data sets
Added var_list option10/28/04 Forced temporary data sets to be uncompressed.
Added var_pre option10/29/04 Now sorts temp. output data sets and merges them by _NAME_.
09/29/10 PAT Makes sure that output data set name is 32 chars or less.
Creates global macro variable _compile_num_desc_out which
has full name of output data set.02/23/11 PAT Added declaration for local macro vars.
for ds_lib=.
Added default value WORK
Added macro testing code.*********************************************************************/
***** ***** ***** MACRO SET UP ***** ***** *****;
%
/** Global macro var for output data set name **/
%global _compile_num_desc_out;
%local out_lib out_name i s files;
** Assume WORK library if blank **;
%%if &ds_lib = %then %let ds_lib = work;
** Shorter file name for temporary data sets **;
%
%if %index( &out_pre._&ds_name, . ) > 0 %then %do;
%let out_lib = %scan( &out_pre._&ds_name, 1, . );
%let out_name = %scan( &out_pre._&ds_name, 2, . );
%end;%else %do;
= work;
%let out_lib = &out_pre._&ds_name;
%let out_name
%end;
** Shorten output data set name if over 32 chars. **;
%
%if %length( &out_name ) > 32 %then %let out_name = %substr( &out_name, 1, 32 );
= &out_lib..&out_name;
%let _compile_num_desc_out
note_mput( macro=Compile_num_desc,
%msg=Output data set name %upcase(&_compile_num_desc_out) saved to global macro variable _COMPILE_NUM_DESC_OUT. )
***** ***** ***** ERROR CHECKS ***** ***** *****;
%
***** ***** ***** MACRO BODY ***** ***** *****;
%
= 1;
%let i %let s = %scan( &stats, &i );
= ;
%let files
%do %while( %length( &s ) > 0 );
=&ds_lib..&ds_name;
proc summary data&var_list;
var =_cnd_&s (drop=_type_ _freq_ compress=no) &s= ;
output out
=_cnd_&s
proc transpose data=_cnd_&s._tr (drop=_label_ rename=(col1=&var_pre._&s) compress=no);
out
=_cnd_&s._tr;
proc sort data
by _name_;
run;
= &files _cnd_&s._tr;
%let files
%let i = %eval( &i + 1 );
%let s = %scan( &stats, &i );
%end;
&_compile_num_desc_out;
data &files;
merge
by _name_;
run;
***** ***** ***** CLEAN UP ***** ***** *****;
%
=work memtype=(data) nolist nowarn;
proc datasets library: ;
delete _cnd_
quit;
%mend Compile_num_desc;
/************************ UNCOMMENT TO TEST ***************************
"Compile_num_desc: SAS Macro";
title ** Autocall macros **;
"K:\Metro\PTatian\UISUG\Uiautos";
filename uiautos =(uiautos sasautos);
options sasautos
options mprint nosymbolgen nomlogic;
data Test;
input id a b;
label = 'Var A'
a = 'Var B';
b
cards;1 1 387
2 2 193
3 3 82
4 4 0
5 5 390
6 6 982
7 7 34
8 8 2
9 9 145
10 10 603
;
run;Compile_num_desc(
%stats=n mean std min max,
ds_lib=,
ds_name=Test,
var_list=a b
)=Test;
proc means data
run;=&_compile_num_desc_out;
proc print data"File = &_compile_num_desc_out";
title2
run;=work memtype=(data);
proc datasets library
quit;/**********************************************************************/
Compile Numeric Statistics
Purpose: Compiles numeric statistics (n, sum, mean, std, min, max) for all variables in a data set. Default is to put statistics in a data set called WORK._DESC_&ds_name.