BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
mgilbert
Obsidian | Level 7

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:

  • I have a data set with 17 variables, some observations are missing datum
  • I would like to create a macro that will loop over the data set and create a new flag variable for each variable in the data set
  • The newly created flag variable(s) will be populated with a 0 or 1 depending on if the observation in the parent variable is missing datum

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

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

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.

PG

View solution in original post

3 REPLIES 3
PGStats
Opal | Level 21

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.

PG
mgilbert
Obsidian | Level 7

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

PGStats
Opal | Level 21
Copy MB to MB_MF and do the update from there.
PG

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 1559 views
  • 1 like
  • 2 in conversation