楼主: dxystata
1565 5

x取值为3 4给个体标记为1 否则为0 [推广有奖]

版主

已卖:302份资源

大师

37%

还不是VIP/贵宾

-

TA的文库  其他...

Software

中英文Ebook

R学习

威望
2
论坛币
183395 个
通用积分
15333.1475
学术水平
208 点
热心指数
271 点
信用等级
174 点
经验
298627 点
帖子
5586
精华
1
在线时间
13632 小时
注册时间
2006-6-21
最后登录
2025-12-22

初级学术勋章 初级热心勋章 中级热心勋章 初级信用勋章

楼主
dxystata 发表于 2016-11-23 22:09:42 |AI写论文
20论坛币
  1. data aaa;
  2. input id trt x;
  3. cards;
  4. 1 1 0
  5. 1 1 1
  6. 1 1 .
  7. 1 1 2
  8. 1 1 2
  9. 2 1 0
  10. 2 1 2
  11. 2 1 3
  12. 2 1 4
  13. 2 1 .
  14. 3 2 0
  15. 3 2 3
  16. 3 2 3
  17. 3 2 4
  18. 3 2 4
  19. 4 2 0
  20. 4 2 .
  21. 4 2 .
  22. 4 2 3
  23. 4 2 .
  24. ;
  25. run;
复制代码

希望得到的数据集id  trt  tag
1   1    0
2   1    1
3   2    1
4   2    1

最佳答案

lovexialulu 查看完整内容

proc sql noprint; create table bbb as select distinct id,trt,count( case when x in (3 4) then id else . end) as tag from aaa group by id trt order by id trt; quit;
关键词:cards Input card Data put

沙发
lovexialulu 发表于 2016-11-23 22:09:43

proc sql noprint;
        create table bbb as
select distinct id,trt,count( case when x in (3 4) then id else . end) as tag
from aaa group by id trt order by id trt;
quit;

藤椅
dxystata 发表于 2016-11-23 22:57:50
lovexialulu 发表于 2016-11-23 22:48
proc sql noprint;
        create table bbb as
select distinct id,trt,count( case when x in (3 4) ...
proc sql noprint;
        create table bbb as
select distinct id,trt,count( case when x in (3 4) then id else . end) as tag
from aaa group by id,trt order by id,trt;
quit;
程序缺了两个逗号。
但结果和我的要求不一样

板凳
Kitty喵喵喵 发表于 2016-11-24 09:37:47
在楼上的基础上增加一步就可以了~
proc sql noprint;
        create table ccc as
        select distinct id ,trt,count(case when x in (3 4) then id else . end) as x1 from aaa
        group by id,trt;
quit;

data ddd;
        set ccc;
        if x1 = 0 then tag = 0;
        else tag = 1;
        drop x1;
run;
已有 1 人评分经验 学术水平 热心指数 收起 理由
dxystata + 100 + 2 + 2 精彩帖子

总评分: 经验 + 100  学术水平 + 2  热心指数 + 2   查看全部评分

报纸
zhangzachary 发表于 2016-11-24 10:49:48
  1. proc sql;
  2.         create table want as
  3.         select id, max(case when x in (3,4) then 1 else 0 end) as tag
  4.         from aaa
  5.         group by 1;
  6. quit;
复制代码

地板
wang1839 在职认证  发表于 2016-11-24 14:33:30

data b;
        set aaa;
        by id;
        retain tag;
        if first.id then tag=0;
        if x in (3,4) then  tag=1;
        if last.id;
run;
已有 1 人评分经验 学术水平 热心指数 收起 理由
dxystata + 100 + 2 + 2 精彩帖子

总评分: 经验 + 100  学术水平 + 2  热心指数 + 2   查看全部评分

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

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