我认为一个简单的问题是,您如何从现有数据中重复调用宏。我会先说,通常你不这样做 - 你通常只是在普通的 SAS 代码中做任何事情。但是你没有给我们任何信息,所以也许你有充分的理由重复调用宏(我有时确实会这样做,尽管通常会试图避免它)。
我会将上面 DavB 的答案合并到这个答案中 - 不要将两个值都作为参数,只使用第一个参数,然后在宏内部将其转换为第二个。
大约有十几种方法可以创建数据驱动的宏调用;这里有三个。
%macro foo(param=);
%let second_param=%sysfunc(translate(¶m,"___","-.a"));
%put ¶m &second_param;
%mend foo;
*Method 1: Call Execute. This puts code in a queue to execute after the data step completes.
This has some advantages, namely that you can build code easily and can modify it with data step functions,
and some disadvantages, namely that some timing issues (when things happen internal to SAS) exist
and can be difficult to remember/work around. Not used all that often as a result of those timing issues
but for simple things can be useful.;
data _null_;
set sashelp.class;
mcall = cats('%foo(param=',name,')');
call execute(mcall);
run;
*Method 2: PROC SQL select into. This creates a list and stores it in a macro variable.
Quick and easy (much shorter code-wise) compared to the other solutions, but has one significant limitation:
a hard limit to how many total characters can be stored in a macro variable (I forget exactly how much, but around 20,000).
So if you are calling the macro a thousand or more times this might fail - it writes a warning to the log if so.
Use NOPRINT with PROC SQL unless you want to see what is generated.;
proc sql;
select cats('%foo(param=',name,')') into :mcalllist separated by ' ' from sashelp.class;
quit;
&mcalllist;
*Method 3: Include file generation. This creates an actual text file with your code in it, then you include that code.
In some ways the most transparent - you can easily debug the code - but also a lot of lines of code to write comparatively.
Does not have the length limitation of the PROC SQL method, although it does have the normal limitations of included code.
The temp file is written to your WORK directory, so you can navigate to that to see the contents, and/or use a non-TEMP file
and write it out to a directory of your choosing in order to see it.
;
filename foo temp;
data _null_;
file foo;
set sashelp.class;
mcall = cats('%foo(param=',name,')');
put mcall $;
run;
%include foo;