Macro project, generalized question

Reply
Occasional Contributor
Posts: 18

Macro project, generalized question

Greetings all,

I have sort of a generalized question, if what I'm what I'm trying to do is possible then if it is possible I will work at it, just need to be pointed in the right direction... 

What I want to do is based off of a certain ID in a sas table say "12ARBOB", or "34BXJIM" I want to run a macro based on the fourth character (R or X).  If it is R then run macro A, if it is X then run macro B. 

Secondly I would need "BOB" or "JIM" to become variables in the Macro that they run in.

Lastly, there are many ID's in this table so I would only want to grab a few based off of a calendar date that has it's own column in the same table as the ID's

I spent most of yesterday reading about array's and %Do statements, would that be the best route for this project?  I've mostly only ever used Proc SQL in SAS so knowing where to start this project is a bit of a head scratcher.... Any direction would be greatly appreciated!

Thanks!mc

Super User
Super User
Posts: 7,942

Re: Macro project, generalized question

Hi,

%macro a (param=); ...

%macro b (param=); ...

data _null_;

     set have;

     if substr(id,4,1)="R" then call execute('%a (param=BOB);');

     else call execute('%b (param=JIM);');

run;

However the real question is why you need to run different macros for different bits of data.  As I always say, in 99% of the cases I come across, thinking about your data and what you want to do in different ways will eliminate the need for this type of operation anyway.  An example:

Say I want to sum age for BOB records and sum height for JIM:

data want;

     set have;

     if index(id,"BOB")>0 then val=age;

     else val=height;

run;

proc sql;

     create table RESULTS as

     select     distinct ID,

                    SUM(VAL) as RESULT

     from        WANT

     group by ID;

quit;

Occasional Contributor
Posts: 18

Re: Macro project, generalized question

Thanks for the reply RW!

The reason why I would want to run different Macro's would be because the macros themselves are big pieces of code that create 30+ tables, so they are pretty granular.  I've thought about what you mentioned though, and what would be the best route to work on this, and likely if I had taken on this project from the beginning I would not have written 7 different programs, but instead one program that has enough data steps in it to work. 

Thanks again!

Ask a Question
Discussion stats
  • 2 replies
  • 151 views
  • 0 likes
  • 2 in conversation