簡體   English   中英

如果表2中存在變量,則SAS在表1中創建標志

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM