楼主: edragon1983
2974 8

[程序分享] 数据格式转换 [推广有奖]

  • 0关注
  • 0粉丝

已卖:1655份资源

讲师

18%

还不是VIP/贵宾

-

威望
0
论坛币
5118 个
通用积分
7.3189
学术水平
0 点
热心指数
3 点
信用等级
0 点
经验
7801 点
帖子
217
精华
0
在线时间
432 小时
注册时间
2007-8-29
最后登录
2025-7-25

楼主
edragon1983 发表于 2010-1-6 16:51:36 |AI写论文
100论坛币
急盼高手相助解决

有这样一批数据,含有2个字符变量X,Y,一个数值变量n。格式如下
X    Y      n
a1   b1    2
a1   b2    0
a1   b3    5
.............
a1  b40   3
a2  b1     3
a2  b2     0
................
a2  b40   1
.................
.................
a60 b1    2
a60 b2    1
................
a60 b40  3

X代表a1-a60,Y代表b1-b40,X与Y一一对应,因此共有60*40=2400种组合,而n则代表每一种组合的数目,例如a1与b1的组合频数为2。

根据这样的数据,我们可以得到
(1)X与Y所有组合的频数,即对n求和N。
(2)X在每个取值时的频数,例如X=a1时的总个数(Y=b1,b2..........b40)
(3)Y在每个取值时的频数,例如Y=b1时的总个数(X=a1,a2..........a60)
(4)X与Y每种组合的数目,即原始n的数值



目标:
准换为含N (N为总频数)条记录,100个变量(a1,a2,a3......a60,b1,b2.......b40)的数据库。100个变量均为布尔逻辑(取值为0或1)。
新数据库为N行,100列。

要求:将原始信息(2)(3)(4)中各数值在新数据库中准确体现。注意:X与Y的组合信息。

关键词:格式转换 数据格式 数值变量 字符变量 数据库 格式转换

沙发
edragon1983 发表于 2010-1-6 17:08:29
11.xls (164.5 KB)
原始数据如附件

藤椅
jingju11 发表于 2010-1-6 23:07:10
2# edragon1983

  1. data a;
  2. do i = 1 to 40;
  3.   x = 'a'||put(i, best.-L);
  4.   do j = 1 to 60;
  5.    y = 'b'||put(j, best.-L);
  6.    n = int(ranuni(1)*5);
  7.    sum = sum(sum, n); *test variable;
  8.    output;
  9.   end;
  10. end;
  11. drop i j;
  12. run;
  13. *the value of SUM in last record should be the number of observations in the final dataset;
  14. data c;
  15. set a(where = (n ^= 0));
  16. array arr(100) a1-a40 b1-b60 (100*0);
  17. s = 0;
  18. do i = 1 to dim(arr);
  19.   arr(i) = (x = vname(arr(i)) or y = vname(arr(i)));
  20.   s+arr(i);
  21.   if s = 2 then leave; *leave the loop earlier;
  22. end;
  23. do i = 1 to n;
  24.   output;
  25. end;
  26. drop i n sum s x y;
  27. run;
复制代码
*I did not use the excel data;

板凳
funnyxuke 发表于 2010-1-6 23:11:06
不知道是不是你要的


data old;
input v1$ v2$ n;
datalines;
a1   b1    2
a1   b2    0
a1   b3    5
a1  b40   3
a2  b1     3
a2  b2     0
a2  b40   1
a60 b1    2
a60 b2    1
a60 b40  3
;
run;

data new;
set old;
do i=1 to n;
output;
end;
run;

option missing=0;
data new1(drop=v1 v2 n i);
set new;
array a{60} a1-a60;
array b{40} b1-b40;
a{substr(v1,2)}=1;
b{substr(v2,2)}=1;
run;

报纸
jingju11 发表于 2010-1-6 23:21:53
4# funnyxuke

oh. Yours is better-faster.


  1. data c;
  2. set a(where = (n ^= 0));
  3. array arr(100) a1-a40 b1-b60 (100*0);
  4. arr(substrn(x, 2)) = 1;
  5. arr(substrn(y, 2)+40) = 1;
  6. do i = 1 to n;
  7.   output;
  8. end;
  9. drop i n sum  x y;
  10. run;
复制代码

地板
funnyxuke 发表于 2010-1-6 23:37:29
4 minutes later than your post.


5# jingju11

7
bobguy 发表于 2010-1-7 07:17:31
Here is the one that will do reasonable fast.

1) The idea is to have the table with all variables a1-a60 + b1-b40 with all values set to '0'.
2) check the observation with n >0
   if it is true, calculate the index for a and b to set the corresponding variables to 1, to repeat n times and to output it.
3) reset the updated variables in 2) to '0'




proc import datafile="C:\Downloads\100106170823e2156bd060f7b3.xls"
                    out=wksht
                    dbms=Excel replace;
run;

data needs;
array a(*) a1-a60;
array b(*) b1-b40;
retain a1-a60 b1-b40 (100*0);
set wksht;

if n>0 then do;
indexa=input(compress(upcase(x),'A'),best.);
indexb=input(compress(upcase(y),'B'),best.);
do i=1 to n;
       a(indexa) =1;
    b(indexb) =1;
    output;
end;
       a(indexa) =0;
    b(indexb) =0;
end;
keep a: b:;
run;

8
edragon1983 发表于 2010-1-7 08:30:42
谢谢各位朋友的热心帮助!谢谢funnyxuke!bobguy!jingju11!
问题已解决!

9
edragon1983 发表于 2010-1-8 09:59:22
版主,怎么把论坛币给热心人啊?

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

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