Sql 如果表2中存在变量,SAS将在表1中创建一个标志

Sql 如果表2中存在变量,SAS将在表1中创建一个标志,sql,sas,compare,flags,Sql,Sas,Compare,Flags,我有两个关于两个班级中每个学生的课程的表,我想在表1中创建一个二进制变量flag,它表示每个学生在表2中的变量课程 表1: class student course 1 A 001 1 A 004 2 B 003 class student course flag 1 A

我有两个关于两个班级中每个学生的课程的表,我想在表1中创建一个二进制变量
flag
,它表示每个学生在表2中的变量
课程

表1:

class   student          course
 1       A                 001
 1       A                 004
 2       B                 003
class   student          course      flag
 1       A                 001         0
 1       A                 004         1
 2       B                 003         1
表2:

class   student          course 
 1       A                 002 
 1       A                 004 
 2       B                 003
预期结果:

表1:

class   student          course
 1       A                 001
 1       A                 004
 2       B                 003
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;
这里有一种方法:

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=dataset选项

 data want ;
   merge table1(in=in1) table2(in=in2);
   by class student course;
   if in1 ;
   flag=in2;
 run;

只需使用MERGE和IN=dataset选项

 data want ;
   merge table1(in=in1) table2(in=in2);
   by class student course;
   if in1 ;
   flag=in2;
 run;

在sql之后,您应该放置一个
,而不是
,和
退出。在sql之后,您应该放置一个
,而不是
,和
退出