楼主: liuliuqiu
3894 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
拉您进交流群

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

GMT+8, 2024-4-26 18:47