在使用SAS的数据步骤中无法使用宏创建变量

时间:2017-07-11 06:28:48

标签: sas sas-macro

data freq;;
    input placebo ;
    cards;
3
    ;
Run;


data freqt;
    set freq;
        %macro freq1 (arm=,pct=,result=);
            %if &arm ne . %then &pct=&arm*100;
        %if &pct ne .  %then %do;
            %if &pct le 10 %then &result = "test";
        %end;
        %mend freq1;
        %freq1(arm=placebo,pct=pct_pla,result=placebo_);
run;

以上数据步骤宏如果条件不起作用但正常条件正常但不需要正常条件。 想只使用宏观条件。请帮帮我。

谢谢...

2 个答案:

答案 0 :(得分:1)

很难说出你想做什么。但是要开发宏,首先需要开始使用SAS代码。也许是这样的事情:

data freqt;
  set freq;
  if placebo ne . then pct_placebo=placebo*100;
  if . <= pct_placebo <= 10 then placebo_= "test";
run;

然后替换随宏变量引用而变化的部分。

%let arm=placebo;
...
  if &arm ne . then pct_&arm=&arm*100;
  if . <= pct_&arm <= 10 then &arm._= "test";

然后定义一个宏。

%macro freq(arm);
if &arm ne . then pct_&arm=&arm*100;
if . <= pct_&arm <= 10 then &arm._= "test";
%mend freq;

然后使用宏。您可能希望多次使用它,否则开始制作宏时不值得。

data freqt;
  set freq;
  %freq(placebo)
  %freq(drug1)
  %freq(drug2)
run;

答案 1 :(得分:1)

您将宏语法与数据步骤语法混淆。在宏语法中,您只能引用宏变量的内容 - 而不是数据步变量。因此,如果&arm包含变量名placebo,那么您的条件

%if &arm ne . %then ...

测试文字placebo是否等于.。当然不是 - 因此总是错误的。

然而,这有效:

if &arm ne . then ...

因为现在您正在编写数据步骤语法,这允许您访问数据步骤变量placebo。现在,您将3.(或其中的任何内容)进行比较。

你还有其他一些问题;宏定义不应该在数据步骤内(它可以把它放在那里,但这样做有点废话)当然你的pct逻辑有同样的问题。