楼主: liuliuqiu
4485 25

[问答] SAS程序请教 [推广有奖]

11
liuliuqiu 发表于 2014-6-27 12:58:57
hello_fj 发表于 2014-6-27 09:42
明白了 你看看是不是这样
太感谢了

12
liuliuqiu 发表于 2014-6-27 14:34:15
hello_fj 发表于 2014-6-27 09:42
明白了 你看看是不是这样
不好意思,我在自己的数据上利用这个程序,得出的结果并不正确呢,能帮我看一下吗?

13
FB_FLORA 发表于 2014-6-27 14:49:31
data sample;
input code year1 year2;
datalines;
1234 2004 2004
2345 2004 2004
3456 2004 2004
3456 2005 2004
4567 2004 2004
4567 2005 2004
5678 2004 2004
5678 2005 2004
5678 2006 2004
;
proc sql;
create table sam1 as
select code , max(year1) as year
from sample
group by code
order by code
;
create table sam2 as
select year, count(distinct code) as num
from sam1
group by year
order by year
;
quit;
data sam21;
set sam2;
by year;
order=_n_;
lorder=_n_+1;
run;
data sam22;
merge sam21(in=a drop=lorder) sam21(drop=order year rename=(lorder=order num=lastnum));
by order;
retain cumnum;
if a;
if missing(lastnum) then lastnum=0;
cumnum+lastnum;
run;
proc sql;
create table sample1 as
select *
from sample a , sam22 b
where a.year1=b.year
;
quit;
data sample2;
set sample1;
if year1=year2 then number=0;
else if year1>year2 then number=lastnum;
run;
proc sort data=sample2;
by year1;
run;

14
liuliuqiu 发表于 2014-6-27 15:00:49
FB_FLORA 发表于 2014-6-27 14:49
data sample;
input code year1 year2;
datalines;
谢谢您。
但得出的结果跟我手算出的结果不一致呢

15
FB_FLORA 发表于 2014-6-27 15:09:36
刚才的有个地方错了,不好意思,这个我算的是一样的
data sample;
input code year1 year2;
datalines;
1234 2004 2004
2345 2004 2004
3456 2004 2004
3456 2005 2004
4567 2004 2004
4567 2005 2004
5678 2004 2004
5678 2005 2004
5678 2006 2004
;
proc sql;
create table sam1 as
select code , max(year1) as year
from sample
group by code
order by code
;
create table sam2 as
select year, count(distinct code) as num
from sam1
group by year
order by year
;
quit;
data sam21;
set sam2;
by year;
order=_n_;
lorder=_n_+1;
run;
data sam22;
merge sam21(in=a drop=lorder) sam21(drop=order year rename=(lorder=order num=lastnum));
by order;
retain cumnum;
if a;
if missing(lastnum) then lastnum=0;
cumnum+lastnum;
run;
proc sql;
create table sample1 as
select *
from sample a , sam22 b
where a.year1=b.year
;
quit;
data sample2;
set sample1;
if year1=year2 then number=0;
else if year1>year2 then number=cumnum;这个地方刚才没改
run;

proc sort data=sample2;
by year1;
run;
已有 1 人评分学术水平 热心指数 信用等级 收起 理由
Tigflanker + 2 + 2 + 2 热心帮助其他会员

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

16
hello_fj 发表于 2014-6-27 15:19:30
liuliuqiu 发表于 2014-6-27 14:34
不好意思,我在自己的数据上利用这个程序,得出的结果并不正确呢,能帮我看一下吗?
哪里有问题?

17
liuliuqiu 发表于 2014-6-27 16:00:53
hello_fj 发表于 2014-6-27 15:19
哪里有问题?
得出的结果跟我随机抽样手算的结果不一致呢。我能把数据发给您,你帮我看一下吗?我知道我这个要求蛮过分的,但实在不知道怎么办了

18
liuliuqiu 发表于 2014-6-27 16:02:42
FB_FLORA 发表于 2014-6-27 15:09
刚才的有个地方错了,不好意思,这个我算的是一样的
data sample;
input code year1 year2;
非常感谢!利用这个例子是做出来了,但是用我自己的数据怎么就做不出来呢?结果明显不正确。

19
liuliuqiu 发表于 2014-6-27 16:23:56
hello_fj 发表于 2014-6-27 09:42
明白了 你看看是不是这样
老师,实在不好意思,我再请教一个问题,如果说hs不止有一种值呢?例如
hs    code    year1   year2       number
1     1234    2004   2004          0
1     2345    2004    2004         0
1     3456    2004    2004         0
1     3456    2005    2004         2
1     4567    2004    2004         0
1     4567    2005    2004         2  
1     5678    2004    2004         0
1     5678    2005    2004         2
1     5678    2006    2004         4
2    .....
2    .....
2   ......
这时对hs=1.2....分别得到以上的结果应该怎么做呢?不好意思,实在是麻烦您了

20
liuliuqiu 发表于 2014-6-27 16:25:10
FB_FLORA 发表于 2014-6-27 15:09
刚才的有个地方错了,不好意思,这个我算的是一样的
data sample;
input code year1 year2;
老师,实在不好意思,结果不一致可能是因为我上例中hs只含有一个值,如果说hs不止有一种值呢?例如
hs    code    year1   year2       number
1     1234    2004   2004          0
1     2345    2004    2004         0
1     3456    2004    2004         0
1     3456    2005    2004         2
1     4567    2004    2004         0
1     4567    2005    2004         2  
1     5678    2004    2004         0
1     5678    2005    2004         2
1     5678    2006    2004         4
2    .....
2    .....
2   ......
这时对hs=1.2....分别得到以上的结果应该怎么做呢?不好意思,实在是麻烦您了

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

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-22 02:38