This is my first post, so apologies if I'm missing something critical. Have spent time on Google and searching the SAS Community boards, but have not been able to find a solution (this one and this one come close though).
This is what's going on:
My interest in using a macro is because I could apply this to a variety of data sets, rather than typing everything out... This is what I have so far:
**********************************************************************;
* Moneyball;
* Last updated: 2015-10-04 by MJG;
**********************************************************************;
* Shorten data name, save to work library;
data MB;
set mydata.moneyball;
run; quit;
***********************************;
* PROC CONTENTS;
***********************************;
* List out the column names and data types for the data set;
proc contents data = MB out = MB_Contents;
run; quit;
* Drop unnecessary variables gained from PROC CONTENTS;
data MB_Contents;
set MB_Contents(keep = name type length varnum format formatl informat
informl just npos nobs);
run; quit;
* View contents of data set, more info than PROC CONTENTS output;
proc print data = MB_Contents;
run; quit;
**********************************************************************;
* Missing Flags;
**********************************************************************;
%let data_og = MB;
%let data_new = MB_MF;
%let contents = MB_Contents;
%let varname = name;
* Macro for missing flags;
%macro MB_Missing(varname);
data &data_new.;
set &data_og.;
if missing(&varname.) then &varname._MF = 1;
else &varname._MF = 0;
run; quit;
%mend;
* Create missing flag variables and populate;
data &data_new.;
do i = 1 to num;
set &contents. nobs = num;
call execute('%MB_Missing('||name||')');
end;
run; quit;
From what I can tell (via the SAS log), the loop actually works and creates the flag variables as intended. The only hang up is that when I do a PROC PRINT of the resulting data set, MB_MF, only the last flag variable created is in there, the rest are not. So my suspicion is that I'm missing an APPEND or even PUT (?) somewhere in there?
I am fairly new to SAS so if there's something in my code that feels like nails on a chalkboard, please let me know!
Thanks,
Michael
You want each iteration to add a new variable, so you should be overwriting the dataset, not starting from the original on each iteration.
* Macro for missing flags;
%macro MB_Missing(varname);
data &data_og.;
set &data_og.;
&varname._MF = missing(&varname.);
run;
%mend;
* Create missing flag variables and populate;
data _null_;
set &contents. ;
call execute('%MB_Missing('||name||');');
end;
run;
(untested)
Note: quit; is ignored after run; except for interactive procedures such as proc reg that accept multiple run; statements.
You want each iteration to add a new variable, so you should be overwriting the dataset, not starting from the original on each iteration.
* Macro for missing flags;
%macro MB_Missing(varname);
data &data_og.;
set &data_og.;
&varname._MF = missing(&varname.);
run;
%mend;
* Create missing flag variables and populate;
data _null_;
set &contents. ;
call execute('%MB_Missing('||name||');');
end;
run;
(untested)
Note: quit; is ignored after run; except for interactive procedures such as proc reg that accept multiple run; statements.
Thank you! Worked perfect, and learned a new way to do the IF-THEN-ELSE logic. Greatly appreciated.
If I wanted to preserve MB (data_og), and simply create a new data set with these additional variables is the best way to do that a data step outside of the macro and populate step?
Michael
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.