楼主: yu9954
2127 10

[有偿编程] 请教:如何用把某一变量名相同的观测值合并成同一行 [推广有奖]

  • 0关注
  • 1粉丝

大专生

73%

还不是VIP/贵宾

-

威望
0
论坛币
820 个
通用积分
2.0765
学术水平
0 点
热心指数
0 点
信用等级
0 点
经验
660 点
帖子
35
精华
0
在线时间
50 小时
注册时间
2018-11-22
最后登录
2023-10-23

20论坛币
我有如下数据:

Study_ID

A01ab

A02ab

A03ab

A04ab

B01ab

B02ab

1

1

0

0

0

0

0

1

0

0

1

0

0

0

1

0

0

0

0

1

0

2

1

0

0

0

0

0

2

0

1

0

0

0

0

原始数据中A01ab,A02ab,A03ab,A04ab,B01ab,B02ab这些变量的值,每一行只可能有一个值为1
想输出如下结果:

Study_ID

A01ab

A02ab

A03ab

A04ab

B01ab

B02ab

1

1

0

1

0

1

0

2

1

1

0

0

0

0



同一个StudyID的A01ab,A02ab,A03ab,A04ab,B01ab,B02ab这些变量的值同时相加(其实就是取这些变量中所有为1的值)并且输出一行观测值

最佳答案

逍遥梦蝶 查看完整内容

重新学习array,又写了一遍。 我的编程思路大致如下: [*]因为最后只要individual level的数据集,而原始数据集的结构属于“multipl rows for an individual”,可以使用 LAST. 功能在每一组的最后一行输出到数据集即可。 [*]利用 DATA 步按行循环处理的原理,对各列进行累计加总。一旦每组最后一行的累加值大于0,即说明该列中有取值为 1 的情况。因为需要累加,所以用到了 SUM 语句。建议先不加“ if last.study_ID the ...
关键词:观测值 如何用 study 原始数据 ABA
沙发
逍遥梦蝶 发表于 2020-4-20 18:31:28 |只看作者 |坛友微信交流群
重新学习array,又写了一遍。

  1. data a;
  2. input study_ID A01ab A02ab A03ab A04ab B01ab B02ab;
  3. cards;
  4. 1 1 0 0 0 0 0
  5. 1 1 0 1 0 0 0
  6. 1 1 0 1 0 1 0
  7. 2 1 0 0 0 0 0
  8. 2 0 1 0 0 0 0
  9. ;
  10. run;

  11. proc sort data=a out=in;
  12.         by study_ID;
  13. run;
  14. proc print data=in;
  15. run;

  16. data out;
  17.         set in;
  18.         by study_ID;
  19.                   array rawvar {*} A01ab A02ab A03ab A04ab B01ab B02ab; /* Re-define the raw variables as elements of an array */
  20.                   array newvar {*} A01 A02 A03 A04 B01 B02; /* Define the new variables as elements of an array */
  21.                                   do i = 1 to dim(rawvar);
  22.                                         if first.study_ID then do; /* Create a new variable and assign an initial value of 0 at each first record of sutdy_ID*/
  23.                                                 newvar{i} = 0;
  24.                                         end;
  25.                                         newvar{i} + rawvar{i}; /* SUM statement, hold the value of last row and then add the value of corresponding raw variable */
  26.                                         if newvar{i} ^= 0 then newvar{i} = 1;
  27.                                   end;
  28.                   if last.study_ID then output;
  29.         keep A01 -- B02;
  30. run;

  31. proc print data=out;        
  32. run;
复制代码

我的编程思路大致如下:
  • 因为最后只要individual level的数据集,而原始数据集的结构属于“multipl rows for an individual”,可以使用 LAST. 功能在每一组的最后一行输出到数据集即可。
  • 利用 DATA 步按行循环处理的原理,对各列进行累计加总。一旦每组最后一行的累加值大于0,即说明该列中有取值为 1 的情况。因为需要累加,所以用到了 SUM 语句。建议先不加“ if last.study_ID then output; ” 这一句,查看生成的数据集是否正确,以便验证编程的逻辑。
  • 最后按需整理数据集,保留所需变量。

使用道具

藤椅
yu9954 发表于 2020-4-20 19:38:02 |只看作者 |坛友微信交流群
数据输入代码如下
data a;
input study_ID A01ab A02ab A03ab A04ab B01ab B02ab;
cards;
1 1 0 0 0 0 0
1 0 0 1 0 0 0
1 0 0 0 0 1 0
2 1 0 0 0 0 0
2 0 1 0 0 0 0
;

使用道具

板凳
yu9954 发表于 2020-4-20 19:39:30 |只看作者 |坛友微信交流群
SAS小白,还望各位大神不吝赐教感谢!

使用道具

报纸
zdl1225 发表于 2020-4-20 19:58:12 |只看作者 |坛友微信交流群
供参考:proc report data=a;
column study_ID (A01ab A02ab A03ab A04ab B01ab B02ab),sum;
define  study_ID/group;
ods output report=work.b(drop=_break_);
run;

使用道具

地板
逍遥梦蝶 发表于 2020-4-21 09:31:17 |只看作者 |坛友微信交流群
  1. data a;
  2. input study_ID A01ab A02ab A03ab A04ab B01ab B02ab;
  3. cards;
  4. 1 1 0 0 0 0 0
  5. 1 0 0 1 0 0 0
  6. 1 0 0 0 0 1 0
  7. 2 1 0 0 0 0 0
  8. 2 0 1 0 0 0 0
  9. ;
  10. run;

  11. proc sort data=a out=in;
  12.         by study_ID;
  13. run;

  14. data out;
  15.         set in;
  16.         by study_ID;
  17.         if first.study_ID then do;
  18.                 A01 = 0;
  19.                 A02 = 0;
  20.                 A03 = 0;
  21.                 A04 = 0;
  22.                 B01 = 0;
  23.                 B02 = 0;
  24.         end;
  25.         A01 + A01ab;
  26.         A02 + A02ab;
  27.         A03 + A03ab;
  28.         A04 + A04ab;
  29.         B01 + B01ab;
  30.         B02 + B02ab;
  31.         if A01 ^= 0 then A01 = 1;
  32.         if A02 ^= 0 then A02 = 1;
  33.         if A03 ^= 0 then A03 = 1;
  34.         if A04 ^= 0 then A04 = 1;
  35.         if        B01 ^= 0 then B04 = 1;
  36.         if B02 ^= 0 then B02 = 1;
  37.         if last.study_ID then output;
  38.         keep A01 -- B02;
  39. run;

  40. proc print data=out;       
  41. run;
复制代码
代码有些长。肯定是可以有更简短的写法的,比如用数组。可是数组的用法还不是很熟悉,看有没有高手来修改一下使它变得更短。

使用道具

7
yu9954 发表于 2020-4-21 13:28:55 来自手机 |只看作者 |坛友微信交流群
逍遥梦蝶 发表于 2020-4-20 18:31
重新学习array,又写了一遍。
谢谢大神指点思路,我自己用了你说的第一种方法,但是还是想学习一下数组的运用,正好你解决了我的疑惑,再次感谢

使用道具

8
jg.sas 发表于 2020-6-8 16:13:14 |只看作者 |坛友微信交流群
基于每一行只可能有一个值为1原则;
data a;
input study_ID A01ab A02ab A03ab A04ab B01ab B02ab;
cards;
1 1 0 0 0 0 0
1 0 0 1 0 0 0
1 0 0 0 0 1 0
2 1 0 0 0 0 0
2 0 1 0 0 0 0
;
run;

proc sql noprint;
        create table b as
        select study_ID,sum(A01ab) as A01ab,sum(A02ab) as A02ab,sum(A03ab) as A03ab,sum(A04ab) as A04ab,sum(B01ab) as B01ab,sum(B02ab) as B02ab
        from a
        group by study_ID;
run;

使用道具

9
zhlewis 发表于 2020-6-12 14:48:43 |只看作者 |坛友微信交流群
  1. data a;
  2. input study_ID $ A01ab A02ab A03ab A04ab B01ab B02ab;
  3. cards;
  4. 1 1 0 0 0 0 0
  5. 1 0 0 1 0 0 0
  6. 1 0 0 0 0 1 0
  7. 2 1 0 0 0 0 0
  8. 2 0 1 0 0 0 0
  9. ;
  10. run;

  11. proc sql;
  12.         create table b as
  13.         select distinct study_id, sum(A01ab) as A01ab, sum(A02ab) as A02ab, sum(A03ab) as A03ab,
  14.                                         sum(A04ab) as A04ab, sum(B01ab) as B01ab, sum(B02ab) as B02ab
  15.         from a
  16.         group by study_id
  17.         ;
  18. quit;
复制代码

使用道具

10
孤单的我们 发表于 2020-6-12 15:27:30 |只看作者 |坛友微信交流群
data a;
input study_ID $ A01ab A02ab A03ab A04ab B01ab B02ab;
cards;
1 1 0 0 0 0 0
1 0 0 1 0 0 0
1 0 0 0 0 1 0
2 1 0 0 0 0 0
2 0 1 0 0 0 0
;
run;

proc sort data=a;
        by study_id;
run;

data b;
        set a;
        by study_id;
        array var A01ab A02ab A03ab A04ab B01ab B02ab;
        array _t{6};
        retain _t1-_t6;
        do i=1 to 6;
                if first.study_id then _t[i]=0;
                if var[i]=1 then _t[i]=1;
                if last.study_id then do;
                        var[i]=_t[i];
                end;
        end;
        if last.study_id;
        drop _t: i;
run;

使用道具

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

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

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

GMT+8, 2024-4-28 05:17