algorithm - LAG函数的更多问题是SAS

标签 algorithm sas lag

下面的 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/

相关文章:

algorithm - token 后缀树教程

sas - 如何通过名称中带有连字符的 SAS libname 引用 SQL Server 表

mysql - 如何使用 SAS 删除特定时间间隔内的第一行或第一组行?

java - 第 K 个元素 N*N 和 vector

algorithm - 增加折扣的渐进非线性算法

Python:查找句子的所有字谜

sql - SAS 和 SQL 的区别

r - 超前或滞后函数可获取多个值,而不仅仅是第n个

python - 当实现两个玩家时 Pygame 会滞后

PHP - MySQL - 选择无限期运行