楼主: Rock2000
1274 11

[有偿编程] 怎样从某数据集中抽取符合另一数据集数据值的所有变量 [推广有奖]

11
Rock2000 发表于 2019-12-3 18:43:05 |只看作者 |坛友微信交流群
wang1839 发表于 2019-12-3 10:56
data a;
input v1 $ v2 $ v3 $ v4 $;
cards;
谢谢,这就是我想要的。

使用道具

12
Rock2000 发表于 2019-12-3 22:11:02 |只看作者 |坛友微信交流群
完整的Code

data a;
input id v1 $10.  v2 $  v3 $ v4 $;
cards;
1 张三        李四  王五     陈六
2 张二        王一   张三    陈一
3 陈六        王二   李四(1) 张二
4 张二        王一   张一    陈一
5 陈一        王一   张三(2) 李二
6 陈一        王一   张二    吴7
7 陈七、张三  吴六   李1     王二
run;

data b;
input V5 $;
cards;
张三
李四
吴7
;
run;

/*方法1*/
/*此方法最直观,但是,如果v5变量含多条记录,编程会变成很长。*/
data c;
set a;
if index(v1,"张三")>0 or index(v2,"张三")>0 or index(v3,"张三")>0 or index(v4,"张三")>0   then yes=1;
if index(v1,"李四")>0 or index(v2,"李四")>0 or index(v3,"李四")>0 or index(v4,"李四")>0   then yes=1;
if index(v1,"吴7")>0  or index(v2,"吴7")>0  or index(v3,"吴7")>0  or index(v4,"吴7")>0    then yes=1;
if yes=1;
run;


/*方法2*/
/*此方法把数据集a的v1-v4变量赋予数组x,再用index()函数找出符合条件的记录*/
data c;
set a;
array x v1-v4;
do i=1 to all;
set b  nobs=all point=i;
do j=1 to dim(x);
if index(x{j},compress(v5))>0 then  output;
end;
end;
drop i j v5;
run;

proc sort data=c out=d nodup;
by id;
run;
      


不知道论坛现在贴的code会变形,详细可以下载附件


Get_data.rar (641 Bytes) 本附件包括:
  • Get_data.sas
  

使用道具

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

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

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

GMT+8, 2024-4-21 00:25