BookmarkSubscribeRSS Feed
Obsidian | Level 7 hdg
Obsidian | Level 7

Hello all, I have another (maybe simple) question. I have the following macro

%macro mytest(dataset,  dataset1, myfldget, myfldon);

proc sql;

create table &dataset as select a.*, b.&myfldget from &dataset a left join &dataset1 b on a.&myfldon = b.&myfldon;



%mytest(myfinal, myfinal, age, date);

I was wondering is there a way I can pass a set of columns to myfldget like age, sales, etc rather than calling this macro again substituing age with sales? THanks very much for your help

Fluorite | Level 6

Try using this

%let a= %str(a.ages,a.sales);

%put &a;

so &a = a.ages, a.sales

You can write all the required fields in a single variable and pass it to the macro.

Hope this helps !

Super User Tom
Super User

I recommend using normal SAS space delimited variable lists when passing them into macros.

So to do what you want you could use this simple macro:

%macro mytest(out,in1,in2,keys,vars);

data &out;

  merge &in1(in=in1) &in2(keep &keys &vars);

  by &keys;

  if in1;


%mend mytest;

Then the call to add both AGE and SEX would be:

%mytest(out=dataset,in1=dataset,in2=dataset1,keys=myfldon,vars=age sex);

Now if you really want to use SQL, don't force the users to type commas and then have to add macro quoting to prevent the commas from confusing the macro call. Just convert the spaces to commas inside the macro.

%macro mytest2(out,in1,in2,keys,vars);

%local varlist join i ;

%let varlist=%sysfunc(compbl(&vars));

%let varlist=b.%sysfunc(tranwrd(&varlist,%str( ),%str(, b.)));

%do i=1 %to %sysfunc(countw(&keys));

  %if &i > 1 %then %let join=&join and ;

  %let join=&join a.%scan(&keys,&i)=b.%scan(&keys,&i) ;


proc sql noprint ;

  create table &out as

    select a.*,&varlist

    from &in1 a left join &in2 b

    on &join



%mend mytest2;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 2 replies
  • 3 in conversation