D*D 发帖数: 236 | 1 想要把一个sorted dataset里,每个subgroup的前三observation输出。
test code 如下
data temp;
do i = 1 to 5;
do j = 1 to 5;
output;
end;
end;
run;
proc sort data = temp; by i j; run;
data new;
set temp;
by i;
if first.i then do;
do k = 1 to 3;
set temp;
by i;
output;
end;
end;
run;
proc print data = new;
run;
结果是
Obs i j k
1 1 1 1
2 1 2 2
3 1 3 3
4 1 4 1
5 1 5 2
6 2 1 3
7 2 2 1
8 2 3 2
9 2 4 3
10 2 5 1
11 3 1 2
12 3 2 3
13 3 3 1
14 3 4 2
15 3 5 3
为什么j输出的range是1-5而不是1-3即使我用了if first.i?
即使if first.i不work,为什么输出的obs是15而不是25?
多谢解答!!! |
D*D 发帖数: 236 | 2 有包子:)
【在 D*D 的大作中提到】 : 想要把一个sorted dataset里,每个subgroup的前三observation输出。 : test code 如下 : data temp; : do i = 1 to 5; : do j = 1 to 5; : output; : end; : end; : run; : proc sort data = temp; by i j; run;
|
D*D 发帖数: 236 | 3 顶
【在 D*D 的大作中提到】 : 想要把一个sorted dataset里,每个subgroup的前三observation输出。 : test code 如下 : data temp; : do i = 1 to 5; : do j = 1 to 5; : output; : end; : end; : run; : proc sort data = temp; by i j; run;
|
j****n 发帖数: 10 | 4 data new (drop=sum);
set temp;
by i;
sum+1;
if not last.i and sum<=3 then output; else sum=0;
run; |
D*D 发帖数: 236 | 5 This is neat. Baozi sent.
Could you point out what's wrong with my code?
【在 j****n 的大作中提到】 : data new (drop=sum); : set temp; : by i; : sum+1; : if not last.i and sum<=3 then output; else sum=0; : run;
|
j****n 发帖数: 10 | 6 slightly changed your code, it works too:
data new;
set temp;
by i;
if first.i then do;
do k = 1 to 3;
/* set temp;*/
/* by i;*/
output;
end;
end;
run; |
j****n 发帖数: 10 | 7 To your old code:
15 records is the right output.
the 1st 'set temp;' Decide the logic true or not (used in the if condisiton)
the 2nd 'set temp;' if the conditin is true, then 3 records will be read.
for the 1st time, the condition is true, then record 1 2 3 will be read.
for the 2nd time, the condition is true, then record 4 5 6 will be read. (
the position of reading the record is not 6, which is the first of subgroup
2).
If you change you code 'first.i' to 'last.i', you will still get 15 records.
Above answer is my colleague's guess, and I think it's the right answer. |
D*D 发帖数: 236 | 8 Thanks for the explanation here. It really helps.
The code you modified was not right though. It printed the same combination
of i and j for 3 times.
What I was looking for was to output the first 3 obs of j, that is j = 1, 2
and 3.
I came to the following code which does the job. Thanks for the discussion.
I have a better understanding of data step now.
data temp;
do i = 1 to 5;
do j = 1 to 5;
output;
end;
end;
run;
proc sort data = temp; by i j; run;
data new (drop = t);
retain t;
set temp;
by i;
if first.i then t=_n_+3;
if _n_
run;
proc print data = new;
run;
output:
Obs i j
1 1 1
2 1 2
3 1 3
4 2 1
5 2 2
6 2 3
7 3 1
8 3 2
9 3 3
10 4 1
11 4 2
12 4 3
13 5 1
14 5 2
15 5 3
condisiton)
subgroup
records.
【在 j****n 的大作中提到】 : To your old code: : 15 records is the right output. : the 1st 'set temp;' Decide the logic true or not (used in the if condisiton) : the 2nd 'set temp;' if the conditin is true, then 3 records will be read. : for the 1st time, the condition is true, then record 1 2 3 will be read. : for the 2nd time, the condition is true, then record 4 5 6 will be read. ( : the position of reading the record is not 6, which is the first of subgroup : 2). : If you change you code 'first.i' to 'last.i', you will still get 15 records. : Above answer is my colleague's guess, and I think it's the right answer.
|