宏返回值 [英] Macro returning a value
问题描述
我创建了以下宏.Proc power
返回包含列 Power
的表 pw_cout
.data _null_
步骤将pw_out
的Power
列中的值赋给宏变量tpw
.我希望宏返回 tpw
的值,以便在主程序中,我可以在 DATA 步骤中调用它,例如:
I created the following macro. Proc power
returns table pw_cout
containing column Power
. The data _null_
step assigns the value in column Power
of pw_out
to macro variable tpw
. I want the macro to return the value of tpw
, so that in the main program, I can call it in DATA step like:
data test;
set tmp;
pw_tmp=ttest_power(meanA=a, stdA=s1, nA=n1, meanB=a2, stdB=s2, nB=n2);
run;
这是宏的代码:
%macro ttest_power(meanA=, stdA=, nA=, meanB=, stdB=, nB=);
proc power;
twosamplemeans test=diff_satt
groupmeans = &meanA | &meanB
groupstddevs = &stdA | &stdB
groupns = (&nA &nB)
power = .;
ods output Output=pw_out;
run;
data _null_;
set pw_out;
call symput('tpw'=&power);
run;
&tpw
%mend ttest_power;
推荐答案
@itzy 指出为什么您的方法行不通是正确的.但是有一个解决方案可以保持您的方法的精神:您需要创建一个使用 PROC FCMP 的功率计算函数.事实上,AFAIK,要从 PROC FCMP 中的函数内部调用过程,您需要将调用包装在一个宏中,所以您差不多了.
@itzy is correct in pointing out why your approach won't work. But there is a solution maintaing the spirit of your approach: you need to create a power-calculation function uisng PROC FCMP. In fact, AFAIK, to call a procedure from within a function in PROC FCMP, you need to wrap the call in a macro, so you are almost there.
这是您的宏 - 稍作修改(主要是为了修复 symput
语句):
Here is your macro - slightly modified (mostly to fix the symput
statement):
%macro ttest_power;
proc power;
twosamplemeans test=diff_satt
groupmeans = &meanA | &meanB
groupstddevs = &stdA | &stdB
groupns = (&nA &nB)
power = .;
ods output Output=pw_out;
run;
data _null_;
set pw_out;
call symput('tpw', power);
run;
%mend ttest_power;
现在我们创建一个函数来调用它:
Now we create a function that will call it:
proc fcmp outlib=work.funcs.test;
function ttest_power_fun(meanA, stdA, nA, meanB, stdB, nB);
rc = run_macro('ttest_power', meanA, stdA, nA, meanB, stdB, nB, tpw);
if rc = 0 then return(tpw);
else return(.);
endsub;
run;
最后,我们可以尝试在数据步骤中使用此函数:
And finally, we can try using this function in a data step:
options cmplib=work.funcs;
data test;
input a s1 n1 a2 s2 n2;
pw_tmp=ttest_power_fun(a, s1, n1, a2, s2, n2);
cards;
0 1 10 0 1 10
0 1 10 1 1 10
;
run;
proc print data=test;
这篇关于宏返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!