SAS中文论坛

 找回密码
 立即注册

扫一扫,访问微社区

查看: 656|回复: 4
打印 上一主题 下一主题

一个分类百分比的问题

[复制链接]

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
楼主
 楼主| 发表于 2008-11-7 22:16:05 | 只看该作者

一个分类百分比的问题

数据aa
id type
1 a
1    a
1    a
2    a
2    a
2    b
2    b
1    b
2    c
2    c
1    c
3    c
3    c
3    a
2    c
现在期望用datastep求每个id下每类type占这类type的百分比,
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
沙发
 楼主| 发表于 2008-11-7 23:43:41 | 只看该作者

Re: 一个分类百分比的问题

why not PROC FREQ?
[code:15tynuju]
proc freq data=a;
table id*type/outpct out=xxx(keep=id type pct_row);
run;
[/code:15tynuju]
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
板凳
 楼主| 发表于 2008-11-8 19:43:32 | 只看该作者

Re: 一个分类百分比的问题

我期望用datastep做
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
地板
 楼主| 发表于 2008-11-8 21:10:20 | 只看该作者

Re: 一个分类百分比的问题

you're so weird! I never see a real need of using ONLY data step.
[code:3npde2yf]
data _null_;
        if 0 then set aa;
        length count 8;
        if _n_=1 then do;
                declare HASH ht(ordered:'a');
                ht.defineKey('id', 'type');
                ht.defineData('id', 'type', 'count');
                ht.defineDone();
        end;
        do until (eof);
                set aa end=eof;
                if ht.find() then
                        ht.add(key: id, key: type, data: id, data: type, data: 1);
                else
                        ht.replace(key: id, key: type, data: id, data: type, data: count+1);
        end;
        ht.output(dataset: 'b');
data xxx;
        n_id=0;
        do until(last.id);
                set b;
                by id;
                n_id++count;
        end;
        do until(last.id);
                set b;
                by id;
                pct_row = count/n_id;
                output;
        end;
run;
[/code:3npde2yf]
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
5#
 楼主| 发表于 2008-11-10 13:58:51 | 只看该作者

Re: 一个分类百分比的问题

[code:2qw85ues]data a;
     input id type $;
         cards;
1 a
1 a
1 a
2 a
2 a
2 b
2 b
1 b
2 c
2 c
1 c
3 c
3 c
3 a
2 c
;
run;

proc sort data=a;
     by id type;
run;

data a b c;
     set a;
         by id type;
         if first.id then tot=0;
         if first.type then num=0;
         num+1 ; tot+1;
         if last.id then output b;
         if last.type then output c;
run;

data final;
     merge c(drop=tot) b(keep=id tot);
         by id;
         pct=num/tot*100;
run;[/code:2qw85ues]
回复 支持 反对

使用道具 举报

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

本版积分规则

QQ|小黑屋|手机版|Archiver|SAS中文论坛  

GMT+8, 2026-2-5 08:48 , Processed in 0.105890 second(s), 20 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表