下面的 SAS 代码应该从一个数据集中读取,该数据集包含一个名为“Radvalue”的数字变量。 Radvalue 是散热器的温度,如果散热器关闭但随后其温度升高 2 或更多,则表明它已经开启,如果它打开但其温度降低 2 或更多则表明它熄灭了。 Radstate 是数据集中的一个新变量,它指示每次观察辐射器是打开还是关闭,这是我试图为整个数据集自动填充的。 所以我尝试使用 LAG 函数,尝试初始化没有 dif_radvalue 的第一行,然后尝试将我刚刚描述的算法应用于第 2 行。 知道为什么列 Radstate 和 l_radstate 完全空白吗?
非常感谢!!如果我没有清楚地解释问题,请告诉我。
Data work.heating_algorithm_b;
Input ID Radvalue;
Datalines;
1 15.38
2 15.38
3 20.79
4 33.47
5 37.03
6 40.45
7 40.45
8 40.96
9 39.44
10 31.41
11 26.49
12 23.06
13 21.75
14 20.16
15 19.23
;
DATA temp.heating_algorithm_c;
SET temp.heating_algorithm_b;
DIF_Radvalue = Radvalue - lag(Radvalue);
l_Radstate = lag(Radstate);
if missing(dif_radvalue) then
do;
dif_radvalue = 0;
radstate = "off";
end;
else if l_Radstate = "off" & DIF_Radvalue > 2 then Radstate = "on";
else if l_Radstate = "on" & DIF_Radvalue < -2 then Radstate = "off";
else Radstate = l_Radstate;
run;
最佳答案
您试图对仅存在于输出数据集 (RADSTATE) 中的变量执行 LAG 函数。我用 RETAIN 替换了 RADSTATE 上的 LAG。此外,您将 LAG 函数保留在任何条件逻辑之外是正确的...请尝试以下代码。
Data work.heating_algorithm_b;
Input ID Radvalue;
Datalines;
1 15.38
2 15.38
3 20.79
4 33.47
5 37.03
6 40.45
7 40.45
8 40.96
9 39.44
10 31.41
11 26.49
12 23.06
13 21.75
14 20.16
15 19.23
;
DATA work.heating_algorithm_c;
length radstate $3;
retain radstate;
SET work.heating_algorithm_b;
old_radvalue=lag(radvalue);
if _n_=1 then do;
dif_radvalue=0;
radstate="off";
end;
else do;
DIF_Radvalue = Radvalue-Old_Radvalue;
if Radstate = "off" & DIF_Radvalue > 2 then Radstate = "on";
else if Radstate = "on" & DIF_Radvalue < -2 then Radstate = "off";
/* Else Radstate stays the same */
end;
run;
关于algorithm - LAG函数的更多问题是SAS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10143745/