0
votes

I have a macro where I am currently passing in 6 table names and 6 columns. However, the number of columns and tables will not always be constant.

Is there a way to have a variable number of parameters? I am familiar with the concept in python with **kwargs.

Also, is there a way to parameterize the proc sql statement to only take as many col and table inputs as provided? Or do a try catch of some sort in SAS to check if the variables exist before running the sql statement?

Here is my macro I'm trying to parameterize.

%macro Generate_TP_tbl(new_tbl_name, trans_col, tbl_1, tbl_2, tbl_3, tbl_4, 
                    tbl_5, tbl_6, col_1, col_2, col_3, col_4, col_5, col_6);

proc sql;

CREATE TABLE &new_tbl_name AS 

SELECT a1._NAME_, a1.&trans_col as &col_1, a2.&trans_col as &col_2, 
       a3.&trans_col as &col_3, a4.&trans_col as &col_4, a5.&trans_col as &col_5, 
       a6.&trans_col as &col_6

FROM &tbl_1 as a1, &tbl_2 as a2, &tbl_3 as a3, &tbl_4 as a4, &tbl_5 as a5, 
     &tbl_6 as a6 

WHERE a1._NAME_ = a2._NAME_ = a3._NAME_ = a4._NAME_ = a5._NAME_ = a6._NAME_;

run;

%mend Generate_TP_table;
2
Use spaces instead of commas in your lists. Then you need just one or two parameters.Tom

2 Answers

3
votes

An even more generic way of doing this is as follows:

%macro mymacro /parmbuff;
  %put &SYSPBUFF;
%mend;

You can then call %mymacro with any parameters you like and parse them all out from the &SYSPBUFF automatic macro variable.

This would probably need more work than Reeza's solution would, but I thought I'd post this anyway for completeness, as it's occasionally useful.

2
votes

Pass them in as a single parameter and have the macro parse them out later.

%macro (parameters = , table_list = tb1 tb2 tb3 ... tb6, col_list=col1 col2 ... col6, other_parms= ... );

I would recommend building the rest of your code using a do loop with the number of parameters. The documentation here has a somewhat bad example of how to extract each element of a list:

http://support.sas.com/documentation/cdl/en/mcrolref/67912/HTML/default/viewer.htm#p1n2i0ewaj1zian1ria5579z1zjh.htm

The SQL is ugly...I wonder if a data step would be easier since you're merging on a single variable? Then it really becomes a rename from each table as in the example above in many respects.