标题: 请教:多个变量排序后first.variable引用的问题 [打印本页] 作者: shiyiming 时间: 2008-4-10 18:11 标题: 请教:多个变量排序后first.variable引用的问题 data aa;
input a b c;
cards;
1 2 1
1 2 2
2 3 3
2 4 4
;
run;
proc sort data=aa out=aaa;
by c b a;
run;
data aaa1;
set aa;
by c b a;
if first.a then output;
run;
data aaa2;
set aa;
by c b a;
if first.b then output;
run;
data aaa3;
set aa;
by c b a;
if first.c then output;
run;
问题:为什么运行出的最后三张数据表(work.aaa1,work.aaa2以及work.aaa3)都是如下形式:
a b c
1 1 2 1
2 1 2 2
3 2 3 3
4 2 4 4
这样的话,first.variable在此岂不是不起作用了吗?
进一步的问题:不知道在面对多个变量排序时,引用first.variable的规则是什么?
<!-- s:( --><img src="{SMILIES_PATH}/icon_sad.gif" alt=":(" title="Sad" /><!-- s:( -->作者: shiyiming 时间: 2008-4-10 19:13 标题: Re: 请教:多个变量排序后first.variable引用的问题 好好学习一下SAS HELP吧
[quote:1oqm0qjl]How SAS Determines FIRST.VARIABLE and LAST.VARIABLE
When an observation is the first in a BY group, SAS sets the value of FIRST.variable to 1 for the variable whose value changed, as well as for all of the variables that follow in the BY statement. For all other observations in the BY group, the value of FIRST.variable is 0. Likewise, if the observation is the last in a BY group, SAS sets the value of LAST.variable to 1 for the variable whose value changes on the next observation, as well as for all of the variables that follow in the BY statement. For all other observations in the BY group, the value of LAST.variable is 0. For the last observation in a data set, the value of all LAST.variable variables are set to 1.[/quote:1oqm0qjl]作者: shiyiming 时间: 2008-4-10 21:47 标题: Re: 请教:多个变量排序后first.variable引用的问题 a b c first.a first.b first.c
1 2 1 1 1 1
1 2 2 0 0 1
2 3 3 1 1 1
2 4 4 0 1 1
这样是不是更清楚点了。作者: shiyiming 时间: 2008-4-11 09:34 标题: Re: 请教:多个变量排序后first.variable引用的问题 谢谢各位 <!-- s:) --><img src="{SMILIES_PATH}/icon_smile.gif" alt=":)" title="Smile" /><!-- s:) --> ,sas学习中,多谢指点作者: shiyiming 时间: 2008-4-11 10:59 标题: Re: 请教:多个变量排序后first.variable引用的问题 上午在大家的建议下,看了帮助文件,逻辑还是有点混乱啊,
“How the DATA Step Identifies BY Groups”
1、How SAS Determines FIRST.VARIABLE and LAST.VARIABLE
When an observation is the first in a BY group, SAS sets the value of FIRST.variable to 1 for the variable whose value changed, as well as for all of the variables that follow in the BY statement. For all other observations in the BY group, the value of FIRST.variable is 0. Likewise, if the observation is the last in a BY group, SAS sets the value of LAST.variable to 1 for the variable whose value changes on the next observation, as well as for all of the variables that follow in the BY statement. For all other observations in the BY group, the value of LAST.variable is 0. [color=#FF0000:ihx9kj61]For the last observation in a data set, the value of all LAST.variable variables are set to 1.[/color:ihx9kj61],
同时这一页的帮助文件还有一句
2、Grouping Observations: Another Example
[color=#FF0000:ihx9kj61]The value of FIRST.variable can be affected by a change in a previous value, even if the current value of the variable remains the same. [/color:ihx9kj61],
问题:结合这句和前面的帮助文件,大体意思是说:first.variable以及last.variable不仅仅收到自身顺序的影响,同时还收到放在它前面的变量顺序的影响,但是具体是什么影响呢?请教一下大家,呵呵。
解决:是不是指在by后面的一系列变量中,前面的始终影响后面的?具体说,就是指前面的一个by变量(假若是a)的观测值改变了,放在它后面的by变量(假若是b),无论针对这一变量(b)的观测值是否改变,first.b和last.b都要随着first.a和last.a变化而做同样的变化?在此基础上,数据集的最后一个观测值的所有变量的last.variable都是1?
不知道我的理解是不是正确的 <!-- s:shock: --><img src="{SMILIES_PATH}/icon_eek.gif" alt=":shock:" title="Shocked" /><!-- s:shock: -->作者: shiyiming 时间: 2008-4-11 11:28 标题: Re: 请教:多个变量排序后first.variable引用的问题 希望这个例子会给你一下帮助。
[quote:90y3iwt8]Region Div First.Region Last.Region First.Div Last.Div
C APTOPS 1 0 1 0
C APTOPS 0 0 0 0
C APTOPS 0 1 0 1
E APTOPS 1 0 1 1
E FINACE 0 0 1 0[/quote:90y3iwt8]
When you use more than one variable in the BY statement, a change in the primary variable forces Last.BY-variable=1 for the secondary variable.