Skip to content

Instantly share code, notes, and snippets.

@statgeek
Last active October 10, 2017 15:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save statgeek/c3a9ddcb002c469e9d61 to your computer and use it in GitHub Desktop.
Save statgeek/c3a9ddcb002c469e9d61 to your computer and use it in GitHub Desktop.
SAS - Missing Macro - For a dataset, variable level report the number of missing and non-missing observations
%macro sum_missing(libname, dsetin, dsetout);
*Delete old dataset;
proc datasets nodetails nolist;
delete &dsetout;
quit;
*Upcase all macro variables to have consistency;
data _null_;
call symput ("libname", upcase("&libname."));
call symput ("dsetin", upcase("&dsetin."));
run;
*Formats for missing for character and numeric variables;
proc format;
value $ missfmt ' '="Missing"
other="Not Missing"
;
value nmissfmt . ="Missing"
other="Not Missing"
;
run;
ods table variablesshort=varshort;
proc contents data=&libname..&dsetin. short;
run;
data _null_;
set varshort;
call symput ("var_list", variables);
run;
proc sql noprint;
*Get count of total number of observations into macro variable;
select count(*) into : obs_count
from &libname..&dsetin.;
quit;
*Start looping through the variable list here;
%let i=1;
%do %while (%scan(&var_list, &i, " ") ^=%str());
%let var=%scan(&var_list, &i, " ");
*Get count of missing;
proc freq data=&libname..&dsetin. noprint;
table &var/missing out=cross_tab1;
format _character_ $missfmt. _numeric_ nmissfmt.;
run;
*Get format of missing;
data _null_;
set cross_tab1;
call symput("var_fmt", vformat(&var));
call symput("var_label", vlabel(&var));
run;
data cross_tab2;
set cross_tab1;
length variable $50.;
category=put(&var, &var_fmt.);
variable="&var_label.";
if _n_=1 and category='Not Missing' then
do;;
Number_Missing=&obs_count-count;
Percent_Missing=Number_Missing/&obs_count.;
percent=percent/100;
output;
end;
else if _n_=1 and category='Missing' then
do;
Number_Missing=count;
Percent_Missing=percent/100;
output;
end;
format percent: percent10.1;
keep variable Number_Missing Percent_Missing;
run;
proc append base=&dsetout data=cross_tab2 force;
run;
proc datasets nodetails nolist;
delete cross_tab1 cross_tab2;
run;
quit;
*Increment counter;
%let i=%eval(&i+1);
%end;
*Categorical;
proc datasets nodetails nolist;
delete step1;
run;
quit;
%mend;
data class;
set sashelp.class;
if age=14 then
call missing(height, weight, sex);
if name='Alfred' then
call missing(sex, age, height);
label age="Fancy Age Label";
run;
%sum_missing(work, class, class_missing);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment