InterviewSolution
Saved Bookmarks
| 1. |
Dataset |
|
Answer» Macro is way of automating in sas programs. There are MANY ways to create macro. %macro get_var_dist (data_nm , categ_cut_off); /* Taking list of variables in the dataset along with type*/ proc contents data=&data_nm. out=_content_; run; /* NOTE - how data_nm is getting resolved. */ /* Fiding number of variables to define loop */ proc sql noprint; select count(*) into: nvars from _content_; quit; /* Note this is another way of DEFINING macro variables */ %put "No of Variables is " &nvars. ; /* Note this can be a useful way to detect the value of macro variables.*/ %do i=1 %to &nvars. ; data _null_; set _content_; if _n_ = &i. then do; call symput("variable", name); call symput("var_type",type); end; run; /* Note - 1: One more way of defining variables. Please note here information is being passed from general SAS dataset to SAS macros using call symput 2: Please see how to run do a loop */ %put "Variable is " &variable. ; %put "Variable Type is " &var_type. ; proc sql noprint; select count(distinct &variable) into: distinct_categories from &data_nm.; quit; %put "Number of distinct categories is &distinct_categories." ; /* Note the syntax of writing if else condition */ /* %do .... %end; defines the block */ /* Missing command below includes missing value as a category */ /* Also note, if you don't use %eval it can throw errors */ /* if comparison involves floating numbers, you will need to use %sasevalf */ %if %eval(&distinct_categories.) <= %eval(&categ_cut_off.) %then %do; Title "Freq Dist for Variable &variable. as number of distinct category is &distinct_categories."; proc freq data=&data_nm. ; table &variable. / missing ; run; %end; %if %eval(&var_type.) = 1 %then %do; Title "Univariate Dist for Variable = &variable. "; proc univariate data=&data_nm. PLOT; var &variable. ; run; %end; %end; %mend get_var_dist; options symbolgen mlogic mprint; %get_var_dist(data_nm=sashelp.heart, categ_cut_off=25); %get_var_dist(data_nm=sashelp.asscmgr, categ_cut_off=10); |
|