Hello all,
Let’s say I wanted to create 4 datasets (let’s call them A1, A2, B1 and B2) that are being processed within the same macro. All calculations are similar except that when the B data sets are being processed, I would like to bypass some calculations that are being done in A. Example:
/*****************/
/*This is not a program, but just an outline of what I would like to get done:*/
%macro dem (data);
data &data.;
set temp;
Create flag for the dataset A. Flag = 0;
else Flag = 1; (which would be dataset B)
If flag = 0 then do; /*calculate variables for dataset A*/
Var1 = x+y; /*same for datasets A and B*/
Var2 = x*y;
Var3 = x*x;
End;
Else If flag = 0 then do; /*calculate variables for dataset A*/
Var1 = x+y; /*same for datasets A and B*/
End;
run;
%mend;
%dem (A1);
%dem (A2);
%dem (B1);
%dem (B2);
/*****************/
I understand that I could just create 2 macros one for processing datasets A1 and A2 (I would actually be done with it already if I took this approach), and a very similar macro for processing datasets B1 and B2.
I am wondering if there is a more elegant way of programming this within the same macro, in that if I’m processing the B datasets the macro should skip some calculations being done in the A datasets (in this case Var2 = x*y; and Var3 = x*x; which are not needed in the B datsets).
Can this be done in SAS? Any help would be greatly appreciated.
Thanks,
Alex
%macro dem (data);
data &data.;
set temp;
Var1 = x+y; /*same for datasets A and B*/
%if %substr(&data,1,1) = B %then %do;
Var2 = x*y;
Var3 = x*x;
%End;
run;
%mend;
%macro dem (data);
data &data.;
set temp;
Var1 = x+y; /*same for datasets A and B*/
%if %substr(&data,1,1) = B %then %do;
Var2 = x*y;
Var3 = x*x;
%End;
run;
%mend;
Or instead of parsing a macro variable name add a parameter to the macro call for type;
%macro dem (data=, type=);
...
%if &type = B %then %do;
Var2 = x*y;
Var3 = x*x;
%End;
...
%mend;
This has a couple of advantages in long term use. If you have your data sets in different libraries you are not going to have to parse around the library name.
Also if you decide to add a type c that has additional differences it is easier to add (or exclude) code sections.
Jerry, your suggestion worked like a charm. Thanks!
Ballardw, I'll have to dig out some macro papers/tutorials in order to implement your suggestion. Which means I definitely have some stuff to learn on macros in SAS. Thank you for your help too.
Best, and enjoy the weekend!
Alex
Interesting.
Let me digest this new information and I'll get back to you guys. Thank you very much for your assistance!
Alex
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.