![](/img/trans.png)
[英]finding which extra columns are present in table1 in comparison with table2
[英]SAS create a flag in table1 if variable is present in table2
我有两个关于每个学生在2个班级中的课程的表格,我想在Table1中创建一个二进制变量flag
,该flag
表示每个学生在Table2中存在变量course
。
表格1:
class student course
1 A 001
1 A 004
2 B 003
表2:
class student course
1 A 002
1 A 004
2 B 003
预期结果:
表格1:
class student course flag
1 A 001 0
1 A 004 1
2 B 003 1
我已经尝试过辅助程序:
proc sql;
create table common as
select A.*, B.*
from Table1 A
inner join Table2 B
on A.class=B.class and A.student=B.student and A.course=B.course;
quit;
那只会输出共同的行,而我没有成功创建一个标志。
希望得到你的答案。 谢谢!
这是一种方法:
proc sql;
create table common as
select a.*,
(case when exists (select 1 from table2 b where A.class=B.class and A.student=B.student and A.course=B.course)
then 1 else 0
end) as flag
from table1 a;
只需使用MERGE和IN =数据集选项。
data want ;
merge table1(in=in1) table2(in=in2);
by class student course;
if in1 ;
flag=in2;
run;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.