全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 SAS专版
4318 6
2012-12-29
在诸如此类语句时。
data three;
  set one two;
  by var;
run;

SAS help中说明:

The values of the variables in the program data vector are set to missing each time SAS starts to read a new
data set and when the BY group changes. (SAS  language reference 9.2, P362)


但我经过测试,我发现其实除了首次将PDV置为缺失时,SAS开始读另一个data set和by 组改变时,均没有置为缺失。

以下是我的测试:
复制代码
log:148  data three;
149  put "before set:" _all_;
150    set one two;
151    by x;
152  put "after set:" _all_;
153  run;

before set:x=. FIRST.x=1 LAST.x=1 _ERROR_=0 _N_=1
after set:x=1 FIRST.x=1 LAST.x=0 _ERROR_=0 _N_=1
before set:x=1 FIRST.x=1 LAST.x=0 _ERROR_=0 _N_=2
after set:x=1 FIRST.x=0 LAST.x=1 _ERROR_=0 _N_=2
before set:x=1 FIRST.x=0 LAST.x=1 _ERROR_=0 _N_=3
after set:x=2 FIRST.x=1 LAST.x=0 _ERROR_=0 _N_=3
before set:x=2 FIRST.x=1 LAST.x=0 _ERROR_=0 _N_=4
after set:x=2 FIRST.x=0 LAST.x=1 _ERROR_=0 _N_=4
before set:x=2 FIRST.x=0 LAST.x=1 _ERROR_=0 _N_=5
after set:x=3 FIRST.x=1 LAST.x=1 _ERROR_=0 _N_=5
before set:x=3 FIRST.x=1 LAST.x=1 _ERROR_=0 _N_=6
after set:x=4 FIRST.x=1 LAST.x=1 _ERROR_=0 _N_=6
before set:x=4 FIRST.x=1 LAST.x=1 _ERROR_=0 _N_=7
NOTE: There were 3 observations read from the data set WORK.ONE.
NOTE: There were 3 observations read from the data set WORK.TWO.
NOTE: The data set WORK.THREE has 6 observations and 1 variables.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds


标红部分都是by组改变,且要切换到另一个data set里读取观测。但是pdv里的值是retain的。没有置为missing.

同样,对于MERGE+by语句:
SAS HELP说明:

When SAS has read all observations in a
BY group from all data sets, it sets all variables in the program data vector
(except those created by SAS) to missing (SAS  language reference 9.2, P373)


复制代码
LOG:
198  data c;
199  put "before set:" _all_;
200    merge a b;
201    by x;
202  put "after set:" _all_;
203  run;

before set:x=. y=  z=  FIRST.x=1 LAST.x=1 _ERROR_=0 _N_=1
after set:x=1 y=a1 z=b1 FIRST.x=1 LAST.x=1 _ERROR_=0 _N_=1
before set:x=1 y=a1 z=b1 FIRST.x=1 LAST.x=1 _ERROR_=0 _N_=2
after set:x=2 y=a2 z=b2 FIRST.x=1 LAST.x=1 _ERROR_=0 _N_=2
before set:x=2 y=a2 z=b2 FIRST.x=1 LAST.x=1 _ERROR_=0 _N_=3
after set:x=3 y=a3 z=b3 FIRST.x=1 LAST.x=1 _ERROR_=0 _N_=3
before set:x=3 y=a3 z=b3 FIRST.x=1 LAST.x=1 _ERROR_=0 _N_=4
NOTE: There were 3 observations read from the data set WORK.A.
NOTE: There were 3 observations read from the data set WORK.B.
NOTE: The data set WORK.C has 3 observations and 3 variables.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds 如上,标红部分也是by组change时,结果还是retain,没有置为Missing.


二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2012-12-29 16:38:32
我的感觉是,SAS在开始读新的一组by-variable-value的时候会将PDV置空,但这种置空不是发生在data步的开始,而是在SET/MERGE语句,也就是读数据的时候

SET BY:
复制代码
x=. y=. FIRST.x=1 LAST.x=1 _ERROR_=0 _N_=1
x=1 y=. FIRST.x=1 LAST.x=0 _ERROR_=0 _N_=1

x=1 y=. FIRST.x=1 LAST.x=0 _ERROR_=0 _N_=2
x=1 y=. FIRST.x=0 LAST.x=1 _ERROR_=0 _N_=2

x=1 y=. FIRST.x=0 LAST.x=1 _ERROR_=0 _N_=3
x=2 y=2 FIRST.x=1 LAST.x=0 _ERROR_=0 _N_=3

x=2 y=2 FIRST.x=1 LAST.x=0 _ERROR_=0 _N_=4
x=2 y=2 FIRST.x=0 LAST.x=1 _ERROR_=0 _N_=4

x=2 y=2 FIRST.x=0 LAST.x=1 _ERROR_=0 _N_=5
x=3 y=. FIRST.x=1 LAST.x=1 _ERROR_=0 _N_=5

x=3 y=. FIRST.x=1 LAST.x=1 _ERROR_=0 _N_=6
x=4 y=4 FIRST.x=1 LAST.x=1 _ERROR_=0 _N_=6

x=4 y=4 FIRST.x=1 LAST.x=1 _ERROR_=0 _N_=7


MERGE BY:
复制代码
1
before set:x=. y=  z=  FIRST.x=1 LAST.x=1 _ERROR_=0 _N_=1
after set:x=1 y=a1 z=b1 FIRST.x=1 LAST.x=1 _ERROR_=0 _N_=1

2
before set:x=1 y=a1 z=b1 FIRST.x=1 LAST.x=1 _ERROR_=0 _N_=2
after set:x=2 y=a2 z=b2 FIRST.x=1 LAST.x=0 _ERROR_=0 _N_=2

3
before set:x=2 y=a2 z=b2 FIRST.x=1 LAST.x=0 _ERROR_=0 _N_=3
after set:x=2 y=a2 z=b22 FIRST.x=0 LAST.x=1 _ERROR_=0 _N_=3

4
before set:x=2 y=a2 z=b22 FIRST.x=0 LAST.x=1 _ERROR_=0 _N_=4
after set:x=3 y=a3 z=b3 FIRST.x=1 LAST.x=1 _ERROR_=0 _N_=4

5
before set:x=3 y=a3 z=b3 FIRST.x=1 LAST.x=1 _ERROR_=0 _N_=5
after set:x=4 y=a4 z=  FIRST.x=1 LAST.x=1 _ERROR_=0 _N_=5

6
before set:x=4 y=a4 z=  FIRST.x=1 LAST.x=1 _ERROR_=0 _N_=6
after set:x=5 y=  z=b5 FIRST.x=1 LAST.x=1 _ERROR_=0 _N_=6

7
before set:x=5 y=  z=b5 FIRST.x=1 LAST.x=1 _ERROR_=0 _N_=7
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2012-12-29 17:23:22
pobel 发表于 2012-12-29 16:38
我的感觉是,SAS在开始读新的一组by-variable-value的时候会将PDV置空,但这种置空不是发生在data步的开始, ...
嗯,跟input及assingned的变量置空的时机不大一样。谢谢啦。
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2012-12-30 14:32:42
PDV机理作用一样,关键在by语句。你可以测试下不用by的set或merge。
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2013-1-5 23:53:14
programing1第292页2001版内容可参考。
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2013-1-6 00:12:26
先看match,是,再看change:是,重置;否,不重置。
否match,重置。
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

点击查看更多内容…
相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群