【问题标题】:SAS create a flag in table1 if variable is present in table2如果变量存在于表 2 中,SAS 在表 1 中创建一个标志
【发布时间】:2016-05-23 15:21:38
【问题描述】:

我有两个关于每个学生在 2 个班级的课程的表格,我想在 Table1 中创建一个二进制变量 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

我已经尝试过 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;

那只输出共同的行,我没有成功创建标志。

希望得到您的答复。谢谢!

【问题讨论】:

    标签: sql sas compare flags


    【解决方案1】:

    这是一种方法:

    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;
    

    【讨论】:

    • 在 sql 之后你应该放一个;,而不是:,最后缺少quit;
    【解决方案2】:

    只需使用 MERGE 和 IN= 数据集选项。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-19
      • 1970-01-01
      相关资源
      最近更新 更多