如果表 2 中存在变量,则 SAS 在表 1 中创建一个标志
SAS create a flag in table1 if variable is present in table2
我有两个关于 2 类 中每个学生的课程的表,我想在表 1 中创建一个二进制变量 flag
,它表示变量 course
的存在每个学生的表 2。
表 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
我试过suivant程序:
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;
我有两个关于 2 类 中每个学生的课程的表,我想在表 1 中创建一个二进制变量 flag
,它表示变量 course
的存在每个学生的表 2。
表 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
我试过suivant程序:
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;