楼主: fanglibing
1645 6

一个特殊数据集的转换 [推广有奖]

  • 0关注
  • 3粉丝

本科生

5%

还不是VIP/贵宾

-

威望
0
论坛币
541 个
通用积分
29.0709
学术水平
7 点
热心指数
8 点
信用等级
6 点
经验
607 点
帖子
36
精华
0
在线时间
106 小时
注册时间
2005-7-25
最后登录
2025-3-19

楼主
fanglibing 发表于 2014-5-30 22:58:48 |AI写论文
10论坛币

请教SAS高手如何实现下面的目标

*基础数据集 x;

data x;

input name$ chinese math music ;

cards;

Tom    1.1 1.2  1.3

Jack   2.1 2.2  2.3

Alice  3.1 3.2  3.3

;

run;

/*希望产生如下数据集 y

包含三个变量student, subject, score

Tom   chinese 1.1

Tom   math    1.2

Tom   music   1.3

Jack  chinese 2.1

Jack  math    2.2

Jack  music   2.3

Alice chinese 3.1

Alice math    3.2

Alice music   3.3

*/

注意数据集x中除姓名name外,课程Chinese, Math, Music可能还包括其他课程,但在x中都是变量名,需要程序自动判断有多少种课程及其名称,然后将课程名列在数据集y的subject变量中。

请高手指教,多谢!

最佳答案

farmman60 查看完整内容

Replace "drop chinese--english; " with "keep name subject score;" or get a macro variable of subjects".
关键词:数据集 Chinese Subject Student Music Chinese music 程序 课程 如何

沙发
farmman60 发表于 2014-5-30 22:58:49
fanglibing 发表于 2014-5-31 18:34
感谢mingfeng07的回答,遗憾的是proc transpose 过程中的var变量列表需要手动确定。
感谢farmman60,程序最 ...
Replace "drop chinese--english; " with "keep name subject score;" or get a macro variable of subjects".

藤椅
mingfeng07 学生认证  发表于 2014-5-31 01:09:43
  1. data x;
  2. input name$ chinese math music ;
  3. cards;
  4. Tom    1.1 1.2  1.3
  5. Jack   2.1 2.2  2.3
  6. Alice  3.1 3.2  3.3
  7. ;
  8. run;
  9. proc transpose data=x out=x1(rename=(name=student col1=score)) name=subject;
  10. var chinese math music;
  11. by descending name;
  12. run;
  13. data x1;
  14. set x1;
  15. label subject=' ';
  16. run;
复制代码
欢迎扫一扫我头像关注, 不定期分享SAS技术知识。

板凳
farmman60 发表于 2014-5-31 04:22:04
  1. data x;
  2. infile cards missover truncover;
  3. input name$ chinese math music physics chemistry english ;
  4. cards;
  5. Tom    1.1 1.2  1.3  . 2.3  .
  6. Jack   2.1 .  2.3 . 2.6 .  
  7. Alice  . 3.2  .  1.3  1.5 1.6
  8. ;
  9. run;

  10. data want;
  11.   set x;
  12.   array vars _numeric_;
  13.   do over vars;
  14.   subject=vname(vars);
  15.   score=vars;
  16.   if not missing(vars) then output;
  17.   end;
  18.   drop chinese--english;
  19.   run;
复制代码

报纸
fanglibing 发表于 2014-5-31 18:34:06
感谢mingfeng07的回答,遗憾的是proc transpose 过程中的var变量列表需要手动确定。
感谢farmman60,程序最大可能的实现了目标,遗憾的是drop语句中的起止变量名需要手动确定。
再次感谢两位大侠!

地板
zhengbo8 发表于 2014-5-31 19:39:58
完美解决,全自动化实现。


  1. data x;
  2.         input name $ chinese math music ;
  3. cards;
  4. Tom    1.1 1.2  1.3
  5. Jack   2.1 2.2  2.3
  6. Alice  3.1 3.2  3.3
  7. ;

  8. proc sql noprint;
  9.         select name into :varlist separated by ' '
  10.         from dictionary.columns
  11.         where libname='WORK' and memname="X" and name <> 'name';
  12. quit;

  13. proc transpose data=x out=y(rename=(name=student _NAME_=subject col1=score));
  14.         var &varlist.;
  15.         by descending name;
  16. run;

  17. data y;
  18.         set y;
  19.         label subject=' ';
  20. run;
复制代码

7
fanglibing 发表于 2014-6-2 16:50:47
感谢zhengbo8,这个程序漂亮,但个人还是感觉farmman60的程序更漂亮。
多谢各位大侠!

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

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2025-12-27 06:34