将等号传递给SAS中的宏 [英] Passing an equals sign to a macro in SAS
问题描述
我正在尝试在数据步骤中生成代码,然后将代码传递到一个宏中,然后该宏将运行该代码.我知道这有点round回,但是我想不出更好的解决方案,因为我的代码内容基于数据集中的内容.
I'm trying to generate code in a data step, and then pass the code into a macro which then runs the code. It's a bit roundabout, I know, but I can't think of a better solution, since the contents of my code are based on what's in the dataset.
在我的数据集"test2"中,我只有一个观察到变量"statement"等于
In my dataset, "test2", I have only one observation of the variable "statement", which is equal to
j1 = input(j,anydtdtm.);放下j;重命名j1 = j; k1 = input(k,anydtdtm.);下降k;重命名k1 = k; l1 = input(l,anydtdtm.);降l;重命名l1 = l;
j1=input(j,anydtdtm.); drop j; rename j1=j; k1=input(k,anydtdtm.); drop k; rename k1=k; l1=input(l,anydtdtm.); drop l; rename l1=l;
我有一个基本上是宏的
%macro dummy(ds,statements);
data &ds.2;
set &ds.;
&statements.
run;
%mend;
然后我使用call execute进行以下操作:
then I use call execute to the following:
data test3;
set test2;
call execute('%dummy('||strip(ds)||','||strip(statement)||')');
run;
但是出现以下错误:
ERROR: The keyword parameter J1 was not defined with the macro.
很显然,SAS会将我的"="符号解释为宏变量的内容以外的其他内容.我尝试使用%str并将调用更改为:
Clearly SAS is interpreting my "=" sign as something other than the contents of a macro variable. I tried using %str and altering my call execute to:
data test3;
set test2;
call execute('%dummy('||strip(ds)||','||%str(strip(statement))||')');
run;
但是没有用.有人有什么想法吗?
but it didn't work. Anyone have any ideas?
感谢您的帮助!
推荐答案
首先,如果您使用命名参数,那么这不是问题.
First off, if you use named parameters, this isn't a problem.
%macro dummy(ds=,statements=);
data &ds.2;
set &ds.;
&statements.
run;
%mend;
data class;
set sashelp.class;
run;
data fixes;
infile datalines truncover;
length statement $40 all_statements $512 execstr $1024;
do _n_ = 1 to 2;
input @1 statement $40.;
all_statements=catx(';',all_statements,statement);
end;
put all_statements;
execstr = cats('%dummy(ds=class,statements=',all_statements,';)');
call execute(execstr);
datalines;
if sex='M' then m_height=height
if sex='F' then f_height=height
;;;;
run;
然后SAS看到命名参数等号,并且知道从该参数到下一个逗号的所有内容都是该参数的值.
Then SAS sees the named-parameter equal sign and knows that everything from that to the next comma is the value of that parameter.
当然,如果您有逗号,您仍然需要做一些事情.这是你亲密无间的地方. %str
需要引用宏调用,而不是宏调用的构造-换句话说,它必须位于引号内.
Of course, if you have a comma, you still need to do something. Here is where you were close-but-not-quite. %str
needs to be quoting the macro call, not the construction of the macro call - in other words, it needs to be inside the quotes.
data fixes;
infile datalines truncover;
length statement $40 all_statements $512 execstr $1024;
do _n_ = 1 to 2;
input @1 statement $40.;
all_statements=catx(';',all_statements,statement);
end;
put all_statements;
execstr = cats('%dummy(ds=class,statements=%nrstr(',all_statements,';))');
call execute(execstr);
datalines;
if sex in ('M','F') then mfheight=height
if sex='F' then f_height=height
;;;;
run;
我个人在这里喜欢%nrstr
,因为它还消除了可能有某些含义的那些讨厌的与号和百分号.
Personally I like %nrstr
here since it also eliminates those pesky ampersands and percent signs, which might have some meaning.
将其放在此处意味着SAS运行调用execute时,它会沿%str
或%nrstr
传递并引用正在发送的值.
Putting it there means that when SAS runs that call execute, it passes along that %str
or %nrstr
and quotes the value that is being sent on.
当您在其他地方拥有它时:
When you had it in the other place:
execstr = cats('%dummy(ds=class,statements=',%nrstr(all_statements),';)');
受保护/引用的不是all_statements
内的文本,而是实际上的字符all_statements
(换句话说,变量名).真的对您没有多大帮助.
What was being protected/quoted was not the text inside all_statements
but actually the characters all_statements
(in other words, the variable name). That doesn't really do much for you.
这篇关于将等号传递给SAS中的宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!