楼主: 040108020007
1512 5

[原创博文] 【求助】一个制作表的问题 [推广有奖]

  • 0关注
  • 0粉丝

讲师

5%

还不是VIP/贵宾

-

威望
0
论坛币
3466 个
通用积分
2.7000
学术水平
0 点
热心指数
1 点
信用等级
0 点
经验
7503 点
帖子
215
精华
0
在线时间
563 小时
注册时间
2007-6-25
最后登录
2024-4-28

30论坛币
data a;input num $11.  id $2. aa$10. ab $10. ar ac;
cards;
13404800000 a 2006-04-01 2007-04-01 800 968
13404800007 b 2006-05-01 2011-04-01 209 1164
13404800012 c 2006-12-19 2013-03-01 38 22
13404800014 c 2006-12-08 2012-08-01 11 1
13404800016 c 2006-12-30 2011-05-01 121 566
13404800020 d 2006-06-30 2010-09-01 136 478
……
;
run;
proc format;
  value fd  0-<10="<10元"
           10-<20=">=10元and<20元"

   20-<50=">=20元and<50元"

   50-<80=">=50元and<80元"

   80-<120=">=80元and<120元"

   120-<200=">=120元and<200元"

   200  <- high=">=200元";
run;
现在想得到以ar为分类变量(用format过程分组如上),id出现的个数(id取值有限,且会重复出现,无论出现多少次都按一次算)和num出现的个数(num取值不会重复,一个代表一个人)。就是一张二维表,列变量是ar.
比如说当ar在200以上这个区间里,id出现了2次(如果a出现多次按一次算),num出现了2个,不同的取值代表不同的人。
我本来是用tabulate做的,但得到的结果中id出现的次数不对,还有就是id和num的所有取值都展开了,但我需要的只是最终加总的结果。
哪位大侠指教一下,谢谢!

最佳答案

soporaeternus 查看完整内容

不知道是不是这个意思
关键词:format Input cards FORMA value 制作

本帖被以下文库推荐

沙发
soporaeternus 发表于 2010-10-13 17:32:15 |只看作者 |坛友微信交流群
  1. data a;input num:$11.  id $1. aa:$10. ab:$10. ar ac;
  2. cards;
  3. 13404800000 a 2006-04-01 2007-04-01 800 968
  4. 13404800007 b 2006-05-01 2011-04-01 209 1164
  5. 13404800012 c 2006-12-19 2013-03-01 38 22
  6. 13404800014 c 2006-12-08 2012-08-01 11 1
  7. 13404800016 c 2006-12-30 2011-05-01 121 566
  8. 13404800020 d 2006-06-30 2010-09-01 136 478
  9. 13404800021 d 2006-06-30 2010-09-01 136 478
  10. ;
  11. run;

  12. proc format;
  13.   value fd  0-<10="<10元"
  14.            10-<20=">=10元and<20元"

  15.    20-<50=">=20元and<50元"

  16.    50-<80=">=50元and<80元"

  17.    80-<120=">=80元and<120元"

  18.    120-<200=">=120元and<200元"

  19.    200  <- high=">=200元";
  20. run;

  21. proc sql;
  22.         create table b as
  23.         select
  24.                 put(ar,fd.) as ar
  25.                 ,count(distinct id) as id_cnt
  26.                 ,count(distinct num) as num_cnt
  27.         from a
  28.         group by
  29.                 calculated ar
  30.         ;
  31. quit;
复制代码
不知道是不是这个意思
已有 2 人评分学术水平 热心指数 信用等级 收起 理由
hopewell + 1 + 1 + 1 我很赞同
040108020007 + 1 + 1 谢谢,不知道该怎么谢了,每次都是你帮助我

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

Let them be hard, but never unjust

使用道具

藤椅
040108020007 发表于 2010-10-13 18:16:00 |只看作者 |坛友微信交流群
你的思路正是我想要的,有一个问题是我输出结果以后,出现了下面的结果,请问这是怎么回事啊?arpu     
>=10元and<20元 1 1
>=20元and<50元 1 1
>=20元and<50元 1 1
>=120元and<200元 2 2
>=200元 1 1
>=200元 1 1

2# soporaeternus

使用道具

板凳
040108020007 发表于 2010-10-13 18:21:48 |只看作者 |坛友微信交流群
原因是我写的时候忘了加calculated了,非常感谢!!! 2# soporaeternus

使用道具

报纸
040108020007 发表于 2010-10-13 18:26:26 |只看作者 |坛友微信交流群
再问一个问题,我不是按ar分类么,输出结果中的ar的顺序乱了,我想让他以最开始我定义的顺序显示,怎么设置一下? 2# soporaeternus

使用道具

地板
soporaeternus 发表于 2010-10-13 21:31:22 |只看作者 |坛友微信交流群
两个类似的方法
第一个
在format的label上手工添加排序号
代码如下
  1. proc format;

  2.   value fd  0-<10="01 <10元"

  3.            10-<20="02 >=10元and<20元"



  4.    20-<50="03 >=20元and<50元"



  5.    50-<80="04 >=50元and<80元"



  6.    80-<120="05 >=80元and<120元"



  7.    120-<200="06 >=120元and<200元"



  8.    200  <- high="07 >=200元";

  9. run;

  10. proc sql;

  11.         create table b as

  12.         select

  13.                 put(ar,fd.) as ar

  14.                 ,count(distinct id) as id_cnt

  15.                 ,count(distinct num) as num_cnt

  16.         from a

  17.         group by

  18.                 calculated ar
  19.                 order by
  20.                                 calculated ar

  21.         ;

  22. quit;
复制代码
第二种方法
增加一个专门用来排序的format
生成汇总表后用data option删除掉
代码如下
  1. proc format;

  2.   value fd  0-<10="<10元"

  3.            10-<20=">=10元and<20元"



  4.    20-<50=">=20元and<50元"



  5.    50-<80=">=50元and<80元"



  6.    80-<120=">=80元and<120元"



  7.    120-<200=">=120元and<200元"



  8.    200  <- high=">=200元";



  9.      value fd_id  0-<10="01"

  10.            10-<20="02"



  11.    20-<50="03"



  12.    50-<80="04"



  13.    80-<120="05"



  14.    120-<200="06"



  15.    200  <- high="07";

  16. run;

  17. proc sql;

  18.         create table b(drop=ar_id) as

  19.         select

  20.                 put(ar,fd.) as ar
  21.                                 ,put(ar,fd_id.) as ar_id

  22.                 ,count(distinct id) as id_cnt

  23.                 ,count(distinct num) as num_cnt

  24.         from a

  25.         group by

  26.                 calculated ar
  27.                                 ,ar_id
  28.                 order by
  29.                                  ar_id

  30.         ;

  31. quit;
复制代码
两种方法各有优劣,你看着用吧......
已有 2 人评分学术水平 热心指数 信用等级 收起 理由
040108020007 + 1 + 1 + 1 精彩帖子
hopewell + 1 + 1 + 1 我很赞同

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

Let them be hard, but never unjust

使用道具

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

本版微信群
加好友,备注cda
拉您进交流群

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

GMT+8, 2024-4-28 22:38