楼主: webgu
4020 6

[学习分享] SAS help错了?关于set/merge +by语句中,pdv变量置为缺失的情况的讨论 [推广有奖]

贵宾

学科带头人

95%

还不是VIP/贵宾

-

TA的文库  其他...

Python与统计

SAS与统计

威望
2
论坛币
102554 个
通用积分
3.6187
学术水平
475 点
热心指数
493 点
信用等级
434 点
经验
62375 点
帖子
1557
精华
4
在线时间
2201 小时
注册时间
2009-5-4
最后登录
2024-4-23

初级学术勋章 初级热心勋章 初级信用勋章 中级学术勋章 中级热心勋章 中级信用勋章

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
在诸如此类语句时。
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 组改变时,均没有置为缺失。

以下是我的测试:
  1. data one;
  2. x=1;output;
  3. x=1;output;
  4. x=3 ;output;
  5. run;

  6. data two;
  7.   x=2;output;
  8.   x=2;output;
  9.   x=4;output;
  10. run;

  11. proc sort data =one;
  12.   by x;
  13. run;
  14. proc sort data=two;
  15.   by x;

  16. data three;
  17. put "before set:" _all_;
  18.   set one two;
  19.   by x;
  20. put "after set:" _all_;
  21. run;
复制代码
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)


  1. data a;
  2. x=1; y='a1';output;
  3. x=2;y='a2';output;
  4. x=3 ;y='a3';output;
  5. run;

  6. data b;
  7.   x=1;z='b1';output;
  8.   x=2;z='b2';output;
  9.   x=3;z='b3';output;
  10. run;

  11. proc sort data=one;
  12.   by x;
  13. run;
  14. proc sort data=two;
  15.   by x;
  16. run;

  17. data c;
  18. put "before set:" _all_;
  19.   merge a b;
  20.   by x;
  21. put "after set:" _all_;
  22. run;
复制代码
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.


二维码

扫码加我 拉你入群

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

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

关键词:Merge Help Set PDV elp one reference values

已有 1 人评分经验 收起 理由
crackman + 100 鼓励积极发帖讨论

总评分: 经验 + 100   查看全部评分

SAS资源
1. SAS 微信:StatsThinking
2. SAS QQ群:348941365
沙发
pobel 在职认证  发表于 2012-12-29 16:38:32 |只看作者 |坛友微信交流群
我的感觉是,SAS在开始读新的一组by-variable-value的时候会将PDV置空,但这种置空不是发生在data步的开始,而是在SET/MERGE语句,也就是读数据的时候

SET BY:
  1. data one;
  2. x=1;output;
  3. x=1;output;
  4. x=3 ;output;
  5. run;

  6. data two;
  7.   x=2;y=2;output;
  8.   x=2;y=2;output;
  9.   x=4;y=4;output;
  10. run;

  11. data three;
  12.    put /_all_;
  13.    set one two;
  14.    by x;
  15.    put _all_;
  16. run;
复制代码
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. data a;
  2. x=1; y='a1';output;
  3. x=2;y='a2';output;
  4. x=3 ;y='a3';output;
  5. x=4; y='a4';output;
  6. run;

  7. data b;
  8.   x=1;z='b1  ';output;
  9.   x=2;z='b2';output;
  10.   x=2;z='b22';output;
  11.   x=3;z='b3';output;
  12.   x=5;z='b5';output;
  13. run;

  14. proc sort data=one;
  15.   by x;
  16. run;
  17. proc sort data=two;
  18.   by x;
  19. run;

  20. data c;
  21. put /_n_/"before set:" _all_;
  22.   merge a b;
  23.   by x;
  24. put "after set:" _all_;
  25. run;
复制代码
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
已有 2 人评分经验 学术水平 热心指数 信用等级 收起 理由
crackman + 100 鼓励积极发帖讨论
webgu + 1 + 1 + 1 精彩帖子

总评分: 经验 + 100  学术水平 + 1  热心指数 + 1  信用等级 + 1   查看全部评分

和谐拯救危机

使用道具

藤椅
webgu 发表于 2012-12-29 17:23:22 |只看作者 |坛友微信交流群
pobel 发表于 2012-12-29 16:38
我的感觉是,SAS在开始读新的一组by-variable-value的时候会将PDV置空,但这种置空不是发生在data步的开始, ...
嗯,跟input及assingned的变量置空的时机不大一样。谢谢啦。
已有 1 人评分经验 收起 理由
crackman + 100 鼓励积极发帖讨论

总评分: 经验 + 100   查看全部评分

SAS资源
1. SAS 微信:StatsThinking
2. SAS QQ群:348941365

使用道具

板凳
zhangg 发表于 2012-12-30 14:32:42 |只看作者 |坛友微信交流群
PDV机理作用一样,关键在by语句。你可以测试下不用by的set或merge。

使用道具

报纸
wangfengxi 发表于 2013-1-5 23:53:14 来自手机 |只看作者 |坛友微信交流群
programing1第292页2001版内容可参考。

使用道具

地板
wangfengxi 发表于 2013-1-6 00:12:26 |只看作者 |坛友微信交流群
先看match,是,再看change:是,重置;否,不重置。
否match,重置。

使用道具

7
kittyforever 发表于 2013-1-6 15:28:17 |只看作者 |坛友微信交流群
sas input和set的pdv设置是不一样的

使用道具

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

本版微信群
加好友,备注cda
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-5-2 02:22