楼主: wanwanle2
1986 6

SAS截取数据得到不同数据集,请大侠解决 [推广有奖]

  • 1关注
  • 22粉丝

已卖:153份资源

博士生

87%

还不是VIP/贵宾

-

威望
0
论坛币
310 个
通用积分
294.0629
学术水平
16 点
热心指数
18 点
信用等级
14 点
经验
287100 点
帖子
306
精华
0
在线时间
370 小时
注册时间
2011-4-13
最后登录
2024-10-23

楼主
wanwanle2 在职认证  发表于 2012-1-4 10:30:49 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
比如我有这样的程序:
data a;
input id x;
cards;
001 a
001 g
003 4
003 g
004 h
;
run;
等等,其中id 不是等间距的,而且其中每个id对应的行数不一定相同,我想把每个id对应的变量分别创建数据集,请大侠指点!谢谢
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:数据集 cards Input card Data 程序 而且

沙发
maidenhan 发表于 2012-1-4 15:18:27
I hope the following code could help.

data a;
input id $ x $;
cards;
001 a
001 g
003 4
003 g
004 h
;
run;

proc sort data = a out = b(keep = id) nodupkey; by id;run;

%macro test();
data _null_;
        set b nobs=n;
        call symput(cats('var',_n_),id);
        if _n_ = n then do;
                call symput('n',n);
                stop;
        end;
run;
data %do ii = 1 %to &n.;
                c_&ii.
         %end;;
        set a;
        select(id);
                %do ii = 1 %to &n.;
                        when (compress("&&var&ii..")) output c_&ii.;
                %end;
                otherwise;
        end;
run;
%mend;
%test();

藤椅
wanwanle2 在职认证  发表于 2012-1-4 18:17:29
这个程序太有用了,非常感谢!thanks!

板凳
shenliang_111 发表于 2012-1-4 23:01:50
  1. data a;
  2. input id $  x $;
  3. cards;
  4. 001 a
  5. 001 g
  6. 003 4
  7. 003 g
  8. 004 h
  9. 005 a
  10. 005 a
  11. 005 t
  12. ;
  13. /*method one-hash*/
  14. data _null_;
  15. length id x $8;
  16. if _n_=1 then do;
  17. declare hash h();
  18. h.definekey('id','x','_n_');
  19. h.definedata('id','x');
  20. h.definedone();
  21. call missing(id,x);
  22. end;
  23. do _n_=1 by 1 until(last.id);
  24. set a;
  25. by id;
  26. rc=h.add();
  27. if last.id  then h.output(dataset:'data'||id);
  28. end;
  29. h.clear();
  30. run;
  31. /*method two-macro*/
  32. data _null_;
  33. set a end=last;
  34. by id;
  35. if first.id then do;
  36.    cnt+1;
  37.    call symputx('data'||put(cnt,2.-l),'data'||id);
  38.    end;
  39. if last then call symputx('totalkind',put(cnt,2.-l));
  40. run;
  41. %macro ss();
  42. data
  43. %do i=1 %to &totalkind.; &&data&i. %end;;
  44. set a;
  45. %do j=1 %to &totalkind.;
  46.     %if %eval(&j. ge 2) %then %do;else %end;
  47.   if 'data'||id="&&data&j." then output &&data&j.;
  48. %end;
  49. run;
  50. %mend;
  51. %ss
  52. /*method three--select when*/
  53. proc sql noprint;
  54. select distinct 'data'||id into :namelist separated by ' '
  55. from a;
  56. select "when('"||id||"') output data"||id into :whenlist separated by ';'
  57. from a;
  58. quit;
  59. data &namelist.;
  60. set a;
  61. select(id);
  62. &whenlist;
  63. otherwise;
  64. end;
  65. run;
  66. /*method four--call execute--效率有待改进*/
  67. data _null_;
  68. set a;
  69. by id;
  70. if last.id then call execute('data  data'||trim(id)||";  set a;where id=('"||trim(id)||"');run;");
  71. run;
复制代码

已有 1 人评分学术水平 热心指数 收起 理由
maidenhan + 1 + 1 强大,几乎给出了所有的方法

总评分: 学术水平 + 1  热心指数 + 1   查看全部评分

报纸
wanwanle2 在职认证  发表于 2012-1-5 22:45:01
感谢大家的帮忙!

地板
jingju11 发表于 2012-1-8 10:06:36
1112345

7
jingju11 发表于 2012-1-8 10:08:11
什么网站?登录了以后居然无法评分!!!
京剧

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

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-1-3 14:48