Help using Base SAS procedures

passing many variables to a single parameter

Reply
Frequent Contributor
Frequent Contributor
Posts: 79

passing many variables to a single parameter

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

Contributor
Posts: 22

Re: passing many variables to a single parameter

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
Super User
Posts: 6,502

Re: passing many variables to a single parameter

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;


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