【发布时间】:2020-12-02 12:43:24
【问题描述】:
我有一个 SAS 表,该表的条件 1 列中嵌入了 if 条件。更明确地说,我创建了一个测试数据集:
data test;
infile datalines delimiter=',';
input x1 x2 flag $ condition1 $ value_cond1_true $ value_cond1_false $ ;
datalines;
1,5, ,x1>x2,A,B
6,5, ,x2>x1,D,A
3,2, , ,C,D
;
run;
我想知道是否可以创建一个可以直接在 SAS 代码中输出 if 语句的代码,而不是为每个观察创建单个宏变量 (&cond1_1, &cond1_2, ... &cond1_n)。
这是我想做的(我知道在这种情况下不可能使用 call symput):
data final;
set test;
/* For each observation */
do i=1 to _n_;
/* Creating macro-variables for the if condition */
call symput("cond1",CONDITION1);
call symput("value_cond1_true",VALUE_COND1_TRUE);
call symput("value_cond1_false",VALUE_COND1_FALSE);
/* If the cond1 macro-variable is not empty then do */
if %sysevalf(%superq(cond1)=, boolean) = 0 then do;
if &cond1. then flag = &value_cond1_true.;
else flag = &value_cond1_false.;
end;
/* If the cond1 macro-variable is empty then */
else flag = "X";
end;
run;
【问题讨论】:
-
先弄清楚您要生成什么代码,然后我们可以向您展示如何从您的输入数据中生成该代码。显示您希望为示例输入运行的代码。显示您希望从该代码中得到什么输出。
-
为什么有一个案例(第3行)缺少
condition1? -
@Richard 可能是缺少条件 1,因此您只需将“X”作为标志。这就是我对最后一个 else 语句的意思。
标签: if-statement sas sas-macro