楼主: lemonxinran
880 2

[有偿编程] 如何批量对数据集进行proc transpose处理 [推广有奖]

  • 0关注
  • 1粉丝

讲师

28%

还不是VIP/贵宾

-

威望
0
论坛币
1159 个
通用积分
6.3000
学术水平
1 点
热心指数
1 点
信用等级
1 点
经验
10334 点
帖子
188
精华
0
在线时间
599 小时
注册时间
2010-10-25
最后登录
2024-3-26

50论坛币
求助各位大神,如何披量对多个数据集进行proc transpose处理。
我有28个构成一模一样的sas格式数据集,数据集名称分别是stock1990,stock1991,stock1992,一直到stock2017.
每个数据集里的变量分别为:
stkcd stkcd1 link
0001  0002    1
0002  0001    1
0002  0003    1
0003  0002    1
现在希望生成这样的3*3矩阵数据:
        0001  0002  0003
0001             1       0
0002    1                 1        
0003     0       1
并将矩阵结果命名为matrix1990,matrix1991,一直到matrix2017输出到excel里。
我大概知道思路是在proc步里调用宏,奈何编程技术太差。还请各位大神赐教,谢谢了。
关键词:Transpose Trans pose RAN SPO
已有 1 人评分经验 收起 理由
eijuhz + 40 精彩帖子

总评分: 经验 + 40   查看全部评分

沙发
gudingji 在职认证  发表于 2019-11-8 10:30:54 |只看作者 |坛友微信交流群
  1. data stock1990;
  2. input stkcd$ stkcd1$ link;
  3. cards;
  4. 0001  0002    1
  5. 0002  0001    1
  6. 0002  0003    1
  7. 0003  0002    1
  8. ;
  9. run;

  10. proc sql;
  11. create table matrix1990 as
  12. select a,max(_0001) as _0001,max(_0002) as _0002,max(_0003) as _0003 from
  13.         (select "0001" as a,(case when stkcd1="0001" then link else . end) as _0001,
  14.                 (case when stkcd1="0002" then link else . end) as _0002,
  15.                 (case when stkcd1="0003" then link else . end) as _0003 from stock1990 where stkcd="0001"
  16.         union
  17.         select "0002" as a,(case when stkcd1="0001" then link else . end) as _0001,
  18.                 (case when stkcd1="0002" then link else . end) as _0002,
  19.                 (case when stkcd1="0003" then link else . end) as _0003 from stock1990 where stkcd="0002"
  20.         union
  21.         select "0003" as a,(case when stkcd1="0001" then link else . end) as _0001,
  22.                 (case when stkcd1="0002" then link else . end) as _0002,
  23.                 (case when stkcd1="0003" then link else . end) as _0003 from stock1990 where stkcd="0003"
  24.         )
  25.         group by a;
  26. quit;

  27. data a;
  28. do i=1 to 28;
  29.         indat=cats("stock",1989+i);
  30.         outdat=cats("matrix",1989+i);
  31.         output;
  32. end;
  33. run;

  34. proc sql;
  35. select indat into:indat separated by "|" from a;
  36. select outdat into:outdat separated by "|" from a;
  37. quit;
  38. %put &indat. &outdat.;

  39. %macro loop();
  40. %do i=1 %to 28;
  41. proc sql;
  42. create table %qscan(&outdat.,&i.,"|") as
  43. select a,max(_0001) as _0001,max(_0002) as _0002,max(_0003) as _0003 from
  44.         (select "0001" as a,(case when stkcd1="0001" then link else . end) as _0001,
  45.                 (case when stkcd1="0002" then link else . end) as _0002,
  46.                 (case when stkcd1="0003" then link else . end) as _0003 from %qscan(&indat.,&i.,"|") where stkcd="0001"
  47.         union
  48.         select "0002" as a,(case when stkcd1="0001" then link else . end) as _0001,
  49.                 (case when stkcd1="0002" then link else . end) as _0002,
  50.                 (case when stkcd1="0003" then link else . end) as _0003 from %qscan(&indat.,&i.,"|") where stkcd="0002"
  51.         union
  52.         select "0003" as a,(case when stkcd1="0001" then link else . end) as _0001,
  53.                 (case when stkcd1="0002" then link else . end) as _0002,
  54.                 (case when stkcd1="0003" then link else . end) as _0003 from %qscan(&indat.,&i.,"|") where stkcd="0003"
  55.         )
  56.         group by a;
  57. quit;
  58. %end;
  59. %mend;

  60. %loop();
复制代码
已有 1 人评分经验 收起 理由
eijuhz + 60 精彩帖子

总评分: 经验 + 60   查看全部评分

使用道具

藤椅
gudingji 在职认证  发表于 2019-11-18 15:54:06 |只看作者 |坛友微信交流群
  1. data stock1990;
  2. input stkcd$ stkcd1$ link;
  3. cards;
  4. 0001  0002    1
  5. 0002  0001    1
  6. 0002  0003    1
  7. 0003  0002    1
  8. ;
  9. run;

  10. proc sql;
  11. create table matrix1990 as
  12. select a,max(_0001) as _0001,max(_0002) as _0002,max(_0003) as _0003 from
  13.         (select "0001" as a,(case when stkcd1="0001" then link else . end) as _0001,
  14.                 (case when stkcd1="0002" then link else . end) as _0002,
  15.                 (case when stkcd1="0003" then link else . end) as _0003 from stock1990 where stkcd="0001"
  16.         union
  17.         select "0002" as a,(case when stkcd1="0001" then link else . end) as _0001,
  18.                 (case when stkcd1="0002" then link else . end) as _0002,
  19.                 (case when stkcd1="0003" then link else . end) as _0003 from stock1990 where stkcd="0002"
  20.         union
  21.         select "0003" as a,(case when stkcd1="0001" then link else . end) as _0001,
  22.                 (case when stkcd1="0002" then link else . end) as _0002,
  23.                 (case when stkcd1="0003" then link else . end) as _0003 from stock1990 where stkcd="0003"
  24.         )
  25.         group by a;
  26. quit;
复制代码

使用道具

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

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

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

GMT+8, 2024-4-25 16:31