楼主: 楚颜错
3186 12

SAS矩阵 [推广有奖]

  • 0关注
  • 3粉丝

已卖:34份资源

博士生

52%

还不是VIP/贵宾

-

威望
0
论坛币
1695 个
通用积分
4.7100
学术水平
2 点
热心指数
2 点
信用等级
0 点
经验
11519 点
帖子
90
精华
0
在线时间
461 小时
注册时间
2012-5-10
最后登录
2023-9-17

楼主
楚颜错 发表于 2015-8-12 09:47:02 |AI写论文
50论坛币
·现在想把一个数据库中变量的值都作为行做一个对应矩阵(观测中有该元素,对应值为1,无则为0),原数据如下:
IDy1y2y3
1AVE
2G
X
3ACR

变换后数据结果如下:
IDAVEGXCR
11110000
20001100
31000011

请问如何操作?谢谢!

最佳答案

chiant 查看完整内容

here is my solution. data a; input ID y1 $2. y2 $2. y3 $2.; cards; 1 A V E 2 G X 3 A C R run; %macro trans_data; proc sql noprint; select y into: value1- from (select distinct y1 as y from a where y1 is not null union select distinct y2 as y from a where y2 is not null union select distinct y3 as y from a w ...
关键词:Dave 如何操作 数据结果 AVE acr 数据库 如何 元素

回帖推荐

sniperhgy 发表于10楼  查看完整内容

说话请不要带节奏哦^_^ 而且不光是处理大数据上,还有就是3个变量就需要union三次,要是10个变量就来十次? 我发下我的解决方案,欢迎斧正:

沙发
chiant 发表于 2015-8-12 09:47:03
here is my solution.

data a;
     input ID y1 $2. y2 $2. y3 $2.;
     cards;
1 A V E
2 G   X
3 A C R
run;

%macro trans_data;
     proc sql noprint;
           select y into: value1-  from
           (select distinct y1 as y from a where y1 is not null
                union select distinct y2 as y from a where y2 is not null
                union select distinct y3 as y from a where y3 is not null)
           ;
     quit;
     data a_new;
           set a;
           array yvar y1-y3;
           %do i=1 %to &sqlobs.;
                &&value&i=0;
           %end;
           do over yvar;
                select(yvar);
                     %do i=1 %to &sqlobs.;
                           when("&&value&i")  &&value&i=1;
                     %end;
                     otherwise;
                end;
           end;
           drop y1-y3;
     run;
%mend;
%trans_data;

已有 1 人评分论坛币 收起 理由
admin_kefu + 20 热心帮助其他会员

总评分: 论坛币 + 20   查看全部评分

藤椅
teqel 发表于 2015-8-13 03:21:00
It works, but is not efficient for a big data.
data a;
input ID y1 $2. y2 $2. y3 $2. ;
cards;
1 A V E
2 G   X
3 A C R
run;
data b (keep=ID A V E G X C R);
set a;
array aa{*} A V E G X C R;
array bb{*} y1-y3;
do i=1 to dim(aa);
        aa=0;
        do j=1 to dim(bb);
                if bb[j]=vname(aa) then do;
                        aa=1;
                        leave ;
                end;
        end;
end;
run;
已有 1 人评分论坛币 学术水平 热心指数 收起 理由
楚颜错 + 2 + 1 + 1 精彩帖子

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

板凳
chiant 发表于 2015-8-13 05:16:11
teqel 发表于 2015-8-13 03:21
It works, but is not efficient for a big data.
data a;
input ID y1 $2. y2 $2. y3 $2. ;
Your code is not only inefficient, but also inflexible. It's not good idea to hard-code the data value (A, V, E, ..., R).

报纸
sniperhgy 发表于 2015-8-13 13:25:52
chiant 发表于 2015-8-13 05:17
here is my solution.

data a;
朋友你的代码确实灵活,但是用distinct这种需要排序的statement外加连用3个select from,要是遇到大一些的数据,也是挺“呵呵”的……

地板
chiant 发表于 2015-8-13 16:31:50
sniperhgy 发表于 2015-8-13 13:25
朋友你的代码确实灵活,但是用distinct这种需要排序的statement外加连用3个select from,要是遇到大一些的 ...
ok, 那您有本事给个大数据的solution呗

7
楚颜错 发表于 2015-8-14 10:25:08
teqel 发表于 2015-8-13 03:21
It works, but is not efficient for a big data.
data a;
input ID y1 $2. y2 $2. y3 $2. ;
谢谢您!

8
楚颜错 发表于 2015-8-14 10:25:43
chiant 发表于 2015-8-12 09:47
here is my solution.

data a;
好厉害,看不懂!谢谢

9
楚颜错 发表于 2015-8-14 10:32:36
sniperhgy 发表于 2015-8-13 13:25
朋友你的代码确实灵活,但是用distinct这种需要排序的statement外加连用3个select from,要是遇到大一些的 ...
确实是大数据~

10
sniperhgy 发表于 2015-8-14 11:31:37
chiant 发表于 2015-8-13 16:31
ok, 那您有本事给个大数据的solution呗
说话请不要带节奏哦^_^

而且不光是处理大数据上,还有就是3个变量就需要union三次,要是10个变量就来十次?

我发下我的解决方案,欢迎斧正:
  1. data a;
  2.      input ID y1 $1. y2 $1. y3 $1.;
  3.      cards;
  4. 1 AVE
  5. 2 G X
  6. 3 ACR
  7. ;
  8. run;

  9. %macro CreateCountTable;
  10.   proc contents
  11.     noprint
  12.     data = a
  13.     out = var_of_a(keep = NAME where = (NAME ne "ID"));
  14.   run;

  15.   data _null_;
  16.     set var_of_a end = eof;
  17.         call symput(strip("VAR") || strip(_N_), strip(NAME));
  18.         if eof then
  19.       call symput(strip("COUNT_OF_VAR"), strip(_N_));
  20.   run;

  21.   data _NULL_;
  22.     if 0 then set A nobs = n;
  23.     call symput('obs_of_a',trim(left(put(n, 8.))));
  24.     stop;
  25.   run;

  26.   %do i = 1 %to &obs_of_a.;
  27.     data _NULL_;
  28.       set a(firstobs = &i. obs = &i.);
  29.           %do j = 1 %to &COUNT_OF_VAR.;
  30.         call symput(strip("VALUE") || strip(&j.), strip(&&VAR&j.));
  31.       %end;
  32.     run;
  33.     %put _user_;
  34.     data part_&i.;
  35.       %do j = 1 %to &COUNT_OF_VAR.;
  36.         %if &&VALUE&j. ne %str() %then
  37.           &&VALUE&j. = 1;;
  38.       %end;
  39.     run;
  40.   %end;

  41.   data wanted;
  42.     set part_1 - part_&obs_of_a.;

  43.     array myArr _numeric_;
  44.         do over myArr;
  45.       myArr = coalesce(myArr, 0);
  46.     end;
  47.   run;

  48.   proc datasets;
  49.     delete part_1 - part_&obs_of_a.;
  50.   run;
  51.   quit;
  52. %mend;

  53. %CreateCountTable;
复制代码
已有 1 人评分论坛币 收起 理由
admin_kefu + 20 热心帮助其他会员

总评分: 论坛币 + 20   查看全部评分

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

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