我只是在搞乱SAS,并决定运行这个简单的程序。
data gnp_growth (keep=date gnp growth);
attrib growth length=8 format=percent8.2;
set sashelp.gnp;
ans = (_n_ = 1);
put _n_= ', ' ans=;
if ans then growth = .;
else growth = log(gnp/lag(gnp));
run;
My log:
_N_=1 , ans=1
_N_=2 , ans=0
...
_N_=126 , ans=0
我的输出多么奇怪
然而,如果我稍微改变这一点,我会得到我想要的输出。我不确定这里发生了什么。 任何人都知道SAS为什么会这样做?提前致谢
data gnp_growth (keep=date gnp growth);
attrib growth length=8 format=percent8.2;
set sashelp.gnp;
ans = (_n_ = 1);
put _n_= ', ' ans=;
lag_gnp = lag(gnp);
if ans then growth = .;
else growth = log(gnp/lag_gnp);
run;
proc print data=gnp_growth; run;
答案 0 :(得分:2)
StackOverflow上已经有这个问题的其他答案,但总结一下:
lag
函数的工作方式就像它们在内部记录在队列类型结构中传递给它们的值一样,并将n
项的值返回队列中(1项返回,对于普通lag()
函数,您也可以调用lag2()
,lag3()
等)。如果您调用lag()
条件,则某些观察值中的值永远不会传递到队列中,从而导致您看到的问题。
答案 1 :(得分:1)
为什么在条件内执行LAG()函数?这将彻底破坏它需要工作的价值流 有关完整说明,请参阅此问题:lag function doesn't work in SAS
试试这个。
lag_gnp=lag(gnp);
if ans then growth = .;
else growth = log(gnp/lag_gnp);