简体   繁体   English

如果表2中存在变量,则SAS在表1中创建标志

[英]SAS create a flag in table1 if variable is present in table2

I have two tables about the course of each student in 2 classes, and I want to create a binary variable flag in Table1 which presents the presence of variable course in Table2 for each student. 我有两个关于每个学生在2个班级中的课程的表格,我想在Table1中创建一个二进制变量flag ,该flag表示每个学生在Table2中存在变量course

Table1: 表格1:

class   student          course
 1       A                 001
 1       A                 004
 2       B                 003

Table2: 表2:

class   student          course 
 1       A                 002 
 1       A                 004 
 2       B                 003

Expected result: 预期结果:

Table1: 表格1:

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

I've tried the suivant program: 我已经尝试过辅助程序:

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;

That only outputs the rows in common and I didn't succeed to create a flag. 那只会输出共同的行,而我没有成功创建一个标志。

Hope to get your answer. 希望得到你的答案。 Thanks! 谢谢!

Here is one method: 这是一种方法:

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;

Just use a MERGE and the IN= dataset options. 只需使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM