0
votes

I wrote a SAS code that allows me to increment a dataset (ds1) with information (var1, var2, var3...) from another dataset (ds2), based on a common key:

proc sql;
    create table ds1 as
    select a.*, b.var1, b.var2, b.var3, b.var4...
    from ds1 as a left join ds2 as b
    on a.key1 = b.key2;
quit;

I want to transform this code into a macro, so I can reuse it. If I had only one information variable (var1), it would be easy:

%macro increment(ds1, key1, ds2, key2, varinfo);
    proc sql;
        create table &ds1. as
        select a.*, b.&varinfo.
        from &ds1. as a left join &ds2. as b
        on a.&key1. = b.&key2.;
    quit;
%mend;

However, I may want to pass multiple information variables:

%increment(ds1, key1, ds2, key2, var1 var2 var3 var4 var5 var6 var7);

How do I rewrite the macro so it works with this "parameter array"?

Thank you!

1
Try %SUPERQ() to quote the string and mask the commas. %superq(var1, var2, var3, var4, var5, var6, var7)Reeza
@Reeza Just commas wouldn't suffice here (since he has to add b. to each of them), so might as well leave them with spaces. Also not sure why you suggest %superq here since they're not variable names - wouldn't %bquote be more appropriate?Joe
@Joe, you're obviously correct. I was using a suggestion from a previous post.Reeza

1 Answers

2
votes

One option is to simply pass it along as you suggest - that will work fine.

%macro increment(ds1, key1, ds2, key2, varinfo);
    proc sql;
        create table &ds1. as
        select a.*
            %do i = 1 %to %sysfunc(countw(&varinfo.));
              ,b.%scan(&varinfo.,&i.)
            %end;
        from &ds1. as a left join &ds2. as b
        on a.&key1. = b.&key2.;
    quit;
%mend;

%increment(ds1, key1, ds2, key2, var1 var2 var3 var4 var5 var6 var7);

Another option, which won't really work any differently in practice but is a more direct answer to the question-as-asked, is to use parmbuff option in the macro declaration, which creates a sort of dummy macro variable out of any parameters after the known ones. But I don't recommend it in this case since you can just use a single parameter.