DATA Step, Macro, Functions and more

processing data differently within the same macro call

Accepted Solution Solved
Reply
Contributor
Posts: 45
Accepted Solution

processing data differently within the same macro call

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


Accepted Solutions
Solution
‎11-15-2013 05:18 PM
Frequent Contributor
Posts: 85

Re: processing data differently within the same macro call

%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


All Replies
Solution
‎11-15-2013 05:18 PM
Frequent Contributor
Posts: 85

Re: processing data differently within the same macro call

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

Super User
Posts: 10,516

Re: processing data differently within the same macro call

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.

Contributor
Posts: 45

Re: processing data differently within the same macro call

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

Contributor
Posts: 45

Re: processing data differently within the same macro call

Interesting.

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

Alex

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 195 views
  • 4 likes
  • 3 in conversation