0
votes

I'm trying to compare if a number of different variables happen in the order I expect using a macro. My code is:

%macro Order (second,first,var);
data order;
set data;
if &second. > &first. then &var._Correct = 1; else &var._Correct = 0;
if &second. < &first. then &var._Error = 1; else &var._Error = 0;
run;
%mend order;
%order(B,A,AB);
%order(C,B,BC);

I have a lot of other variables to compare. The problem is, when I run the macro the output dataset only has the last pair. In this example, that would be BC. I know I can make multiple output datasets and each one would have the pairs, but then I'd have to rejoin them all together. How can I get one dataset that has all of my &var._Correct and &var._Error pairs?

1

1 Answers

1
votes

Your problem is that you're rewriting the data step twice. That's unneeded. Most of the time, macros like this can be lines in a data step not whole data steps.

%macro Order (second,first,var);
if &second. > &first. then &var._Correct = 1; else &var._Correct = 0;
if &second. < &first. then &var._Error = 1; else &var._Error = 0;
%mend order;

data order;
set data;
%order(B,A,AB);
%order(C,B,BC);
run;

Something more like that. I'd note a few minor issues here. What if &second=&first? You want no correct and no error, or is that correct or error?

And an easier way to do this:

%macro Order (second,first,var);
  &var._correct = (&second. > &first.); *or GE?;
  &var._error   = (&second. < &first.); *or LE?; *only one of these two;
%mend order;

That puts the same values into the variable in a lot less code.