| |

Learning Advanced SAS from a Macro: Part 2

Okay, where we left off on the propensity score macro from Feng, Wu and Xu and the nifty things you can learn from reading someone else’s code, in this case, their propensity score macro with calipers ….

We previously dealt with the situation where you had no matches and exactly one match. If you find more than one match within the caliper, you would then go on to the next step. This is where it gets really interesting.

Nifty things eight and nine

%if  %eval(&casen)=0 %then %do;

This shows you how to set up a loop that does certain DATA and PROC steps repeatedly. Once you start using macro %DO loops you will wonder how you ever survived without them. In this first loop, it is going to do these things if there is no matching record for our case, delete the record if there is no matching id, and set the new id equal to i (the counter variable defined way above in the previous post). Notice that you need %IF , %THEN and %DO. I always leave off at least one of those % signs.

This also uses the %EVAL function again, so it is a nice reminder if you are new to macros. This function evaluates (hence the name) the value in the parentheses as a number, not as text. That &casen is the result of a previous PROC SQL step that put the count of the number of records into that macro variable.

So … essentially, if we don’t find a match, we delete that record, re-set our newid value to be one more and loop back up to the top of the data set where we will get the next record and go again.

data case_ctrl_together;
set case_ctrl_together match_temp(in=a keep=&id);
if &id=. then delete;
if a then newid=&i;
run;
%end;

kaleMinor Nifty Thing Ten  …. if you like SQL, which I don’t particularly, but it is probably good for you, like kale

So, here is the %ELSE, followed by the %IF,  %THEN and %DO which does the exact same thing as an ELSE IF  blah blah blah THEN DO ; in  non-macro SAS. There is our %EVAL again (repetition is good when you are first learning).
%else %if %eval(&casen)=1 %then %do;

So, yeah, what exactly do we do if we do have exactly one match? First, you temporarily create a data set of the controls and matches. Next you add the match you have found in to the case_ctrl_together data set you are building

We use a PROC SQL step to update our temporary data set of cases that do not have matches.

I have to say, this is not how I would have done this, but it is good in a way because it does show one way of solving this problem of updating the data set of matches you are building and updating the file to be matched using SQL if you are into that sort of thing. By the way, if you aren’t familiar with SQL, do notice that you need the QUIT ; statement at the end to exit out of it and go back to regular SAS syntax.
data match_temp1;
set ctrl_temp match_temp;
newid=&i;
run;
data case_ctrl_together;
set case_ctrl_together match_temp1(keep=&id newid);
if &id=. then delete;
run;
proc sql;
create table case_temp
as select a.*
from case_temp a, match_temp b
where a.&id^=b.&id;
quit;
%end;

Nifty thing eleven and I really do like this …

If there is more than one match, the macro calls ANOTHER macro which finds the optimal match of those available within the calipers. It does not need the &&macrovar notation to resolve nested macro variables because none of the parameters passed to it are macro variables. They are all ‘hard-coded’ into the original macro. I thought this was rather clever.

I know I have said before that I am not a fan of the unconditional ELSE statement , even under the best of circumstances (and this is the best of circumstances), but, oh well.
%else %do;
***please specify the key variables (including propensity score as one key variable) which are used to calculate the mahalanobis distance ****;
%Mahalanobis(match_temp, prob, ctrl_temp);
%end;
%end;

There are two %END statements because the first one ends the %ELSE %DO that will happen when you have more than one match and the second ends the whole loop that you are doing from 1 to the total number of observations in the control group data set.

I really  liked the %Mahalanobis macro, so tune in next time and see if you do as well.

For now, having just met several deadlines in the past two weeks, I’m going to put my feet up, drink tea and read random blogs on Blogher just for the hell of it.

Similar Posts

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *