BookmarkSubscribeRSS Feed
hdg
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;

run;

%mend;

%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

2 REPLIES 2
yeshwanth
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 !

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

run;

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

%end;


proc sql noprint ;

  create table &out as

    select a.*,&varlist

    from &in1 a left join &in2 b

    on &join

  ;

quit;

%mend mytest2;


sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

Register now!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1076 views
  • 0 likes
  • 3 in conversation