由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Statistics版 - 求教一个简单的data step 牛肉包
相关主题
如何比较两个proc contents的结果?SAS DATA 求助
[SAS] data set options (obs=) in output tablessas adv 63题 52
[提问]怎样sort这个dataset?How to Macro it in SAS?
SAS dataset 中,怎么把数据往上移一行?SAS问题请教
从大data 产生多个小data 的方法A question in splitting dataset
SAS questionSAS菜鸟请教如果使SAS的output的结果放到一个文件内?
请教SAS中如何如果flag的问题which route in SAS is faster?
请教一sas codeWhat's the wrong with this SAS code?
相关话题的讨论汇总
话题: temp话题: data话题: run话题: end话题: obs
进入Statistics版参与讨论
1 (共1页)
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.

1 (共1页)
进入Statistics版参与讨论
相关主题
What's the wrong with this SAS code?从大data 产生多个小data 的方法
PROC SQL join data helpSAS question
a SAS question in base 70请教SAS中如何如果flag的问题
SAS -proc transpose 急问!请教一sas code
如何比较两个proc contents的结果?SAS DATA 求助
[SAS] data set options (obs=) in output tablessas adv 63题 52
[提问]怎样sort这个dataset?How to Macro it in SAS?
SAS dataset 中,怎么把数据往上移一行?SAS问题请教
相关话题的讨论汇总
话题: temp话题: data话题: run话题: end话题: obs