SAS中文论坛

 找回密码
 立即注册

扫一扫,访问微社区

查看: 1727|回复: 3
打印 上一主题 下一主题

lag 函数求教

[复制链接]

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
楼主
 楼主| 发表于 2003-10-30 22:50:41 | 只看该作者

lag 函数求教

数据集:myset
Obs    STKCD      ACCPER

                                      1    000003    19911231
                                      2    000003    19921231
                                      3    000003    19981231
                                      4    000003    19991231
                                      5    000003    20001231
                                      6    000003    20011231
                                      7    000004    19921231
                                      8    000004    19931231
                                      9    000004    19941231
                                     10    000004    19951231
                                     11    000004    19961231
                                     12    000004    19971231
data myset1;
    set myset;
          if stkcd=lag(stkcd) and accper-lag(accper)=10000 then do;
               stkcd1=lag(stkcd);
              accper1=lag(accper);
              end;
       
run;
结果为
                 Obs    STKCD      ACCPER     stkcd1     accper1
                  1    000003    19911231                     .
                           2    000003    19921231                     .
                           3    000003    19981231                     .
                           4    000003    19991231    000003    19921231
                           5    000003    20001231    000003    19991231
                           6    000003    20011231    000003    20001231
                           7    000004    19921231                     .
                           8    000004    19931231    000003    20011231
                           9    000004    19941231    000004    19931231
                          10    000004    19951231    000004    19941231
                          11    000004    19961231    000004    19951231
                          12    000004    19971231    000004    19961231
为什么第4个观测和第8个观测的stkcd1、accper1不是上一个观测的stkcd和accper值呢?
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
沙发
 楼主| 发表于 2003-10-31 00:48:43 | 只看该作者
Because the LAG function stores values on the queue only when it is called, [b:bc182]you must call LAG unconditionally to get the correct answers[/b:bc182].

Example: Storing Every Other Lagged Value This example shows the difference in output when you use conditional and unconditional logic in your program.

options pagesize=25 linesize=64 nodate pageno=1;
title 'Store Every Other Lagged Value';
data test;
input x @@;
if mod(x,2)=0 then a=lag(x);
b=lag(x);
if mod(x,2)=0 then c=b;
label a='(WRONG) a' c='(RIGHT) c';
datalines;
1 2 3 4 5 6 7 8
;
proc print label data=test;
run;


*********************************************
正确写法应为:

[color=red:bc182]data myset1;
set myset;
test1=lag(stkcd);
test2=lag(accper);
if stkcd=test1 and dif(accper)=10000 then do;
stkcd1=test1;
accper1=test2;
end;
drop test1 test2;
run;[/color:bc182]
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
板凳
 楼主| 发表于 2003-10-31 10:43:13 | 只看该作者

多谢,明白了些

多谢!
是不是在条件表达式中的LAG函数值为真实的上一个观测值
而在赋值语句中的LAG函数值为queue中的上一个值。
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
地板
 楼主| 发表于 2003-10-31 12:59:30 | 只看该作者
是这样的。下面程序可以验证:

data a;
input x@@;
cards;
1 2 3 4 5 6 7 8 9
;
run;
data b;
set a;
if lag(x)>4 then do;
ok=1;
lagx=lag(x);
end;
else ok=0;
run;
proc print data=b;run;
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|手机版|Archiver|SAS中文论坛  

GMT+8, 2024-5-18 12:56 , Processed in 0.161518 second(s), 20 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表