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

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

1 ACCEPTED SOLUTION

Accepted Solutions
JerryLeBreton
Pyrite | Level 9

%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;

View solution in original post

4 REPLIES 4
JerryLeBreton
Pyrite | Level 9

%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;

ballardw
Super User

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.

avbraga
Calcite | Level 5

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

avbraga
Calcite | Level 5

Interesting.

Let me digest this new information and I'll get back to you guys. Thank you very much for your assistance!

Alex

sas-innovate-2024.png

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.

 

Register now!

How to Concatenate Values

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.

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
  • 4 replies
  • 731 views
  • 4 likes
  • 3 in conversation