SAS中文论坛

 找回密码
 立即注册

扫一扫,访问微社区

查看: 985|回复: 7
打印 上一主题 下一主题

如何用transpose 来转换数据格式

[复制链接]

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
楼主
 楼主| 发表于 2006-8-18 16:41:14 | 只看该作者

如何用transpose 来转换数据格式

我的数据格式原本是这样:
id services quantity totalcosts
1  A  3   70
1  B  9   140
1  C  1   5
2  B  4   30
2  C  3   23
3  A  2   40
3  C  2   49

想用transpose 来转换下列数据格式:
id service_A quantity_A totalcosts_A service_B quantity_B totalcosts_B service_C quantity_C totalcosts_C

1  1 (注:1 表示有用过这项服务,0 表示没有用过这项服务) 3   70   1  9  140  1  1  5

2  0  0  0    1  4  30  1  3  23   
3  1  2  40  0  0  0    1  2  49

谢谢
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
沙发
 楼主| 发表于 2006-8-19 00:39:32 | 只看该作者

我手持钢鞭将你打

[code:da39d]data ahuige;
  input id services$ quantity totalcosts ;
  cards;
1 A 3 70
1 B 9 140
1 C 1 5
2 B 4 30
2 C 3 23
3 A 2 40
3 C 2 49
;
run;

proc sort ;
  by id ;
run;

data final(drop=quantity totalcosts services);
  set ahuige;
  by id;
  array temp service_A quantity_A totalcosts_A service_B quantity_B totalcosts_B service_C quantity_C totalcosts_C ;
  array quant(65:67) quantity_A quantity_B quantity_C(0,0,0) ;
  array totalc(65:67) totalcosts_A totalcosts_B totalcosts_C(0,0,0);
  array serv(65:67) service_A service_B service_C(0,0,0);
  if first.id then
    do over temp;
      temp=0;
    end;
  quant(rank(services))=quantity;
  totalc(rank(services))=totalcosts;
  serv(rank(services))=1;
  if last.id then output;
run;

proc print;run;[/code:da39d]
我手持钢鞭将你打
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
板凳
 楼主| 发表于 2006-8-19 11:50:44 | 只看该作者

Thanks

Very useful and doable. thanks
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
地板
 楼主| 发表于 2006-8-19 14:57:34 | 只看该作者

detailed explanation

Hi there, bother you again.
your SAS codes are quite complicated. Where can I get the detailed explanation for each step?

thanks.
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
5#
 楼主| 发表于 2006-8-19 17:23:38 | 只看该作者

just do it

actually.
I do not think it is complicated.
if you really want to use a word to describe it I suggest you to use 'Straight'.

1. [quote:21f1a]rank(services)[/quote:21f1a] is used to determine which variable you should pass values to.
2. [quote:21f1a]if last.id then output; [/quote:21f1a] is to output record in a batch.
3.  [quote:21f1a]do over temp[/quote:21f1a] is to eliminate the previous existing values in the last group.
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
6#
 楼主| 发表于 2006-8-19 21:03:57 | 只看该作者

very thanks

thank you very much. I am just a newuser of SAS so...

Could I trouble you to explain why you add (0,0,0) for each of array except the first one (i.e. temp.), thanks
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
7#
 楼主| 发表于 2006-8-19 21:31:10 | 只看该作者

help

refer to the SAS help of ARRAY and the default value
回复 支持 反对

使用道具 举报

49

主题

76

帖子

1462

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1462
8#
 楼主| 发表于 2006-8-19 22:48:13 | 只看该作者

Thanks

Hi Thank you very much for your help and useful information.
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-6-10 15:31 , Processed in 0.079319 second(s), 23 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

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