如何在SQL / SAS中检查空值

时间:2019-06-19 13:09:17

标签: sql sas

我有2张Excel工作表。

 Excel_Sheet1:  
 subject | T/F?  
 001     |  T  
 002     |  F  

 Excel_Sheet2:  
 subject | SiteId  | ReferenceId
 001     |         |     55
 002     |   44    |     66

预期输出:

subject | SiteId   | ReferenceId
001     |          |     55

这是我所做的,但是没有给出正确的输出。我目前正在尝试通过在SAS中使用SQL来实现这一目标,但是我乐于听取通过SAS实现此目标的方法。

我正在尝试首先检查Excel_sheet1中是否为True,然后检查Excel_sheet2中的所有列是否全部为空。如果所有这些特定列均为空,则返回Excel_sheet2的ReferenceId

/*
 *  Import files Excel_sheet1 as s1, Excel_sheet2 as s2
 */



proc sql;

/*
 *  Create table and add 
 */
CREATE table output (
    subject int, 
    SiteId int,
    RecordId int

);


INSERT INTO output
SELECT distinct s1.subject, s1.SiteId, s1.RecordId 
FROM mylib.sheet1 as s1 FULL OUTER JOIN mylib.sheet2 as s2 
ON s1.subject = s2.subject
WHERE s2.SiteId is Null
;
quit;


/* export as excel */

%put &output;   

谢谢

2 个答案:

答案 0 :(得分:1)

仅将正确的数据类型对齐,因为 subject 是字符列(带有零),而不是数字。此外,请在s2.SiteId, s2.RecordId子句中使用SELECT更正别名。

proc sql;
    /*
     *  Create table and add 
    */
    CREATE TABLE output (
        subject varchar(3),   
        SiteId int,
        RecordId int
    );

    INSERT INTO output
    SELECT distinct s1.subject, s2.SiteId, s2.ReferenceId 
    FROM Excel_Sheet1 as s1 
    FULL OUTER JOIN Excel_Sheet2 as s2 
    ON s1.subject = s2.subject
    WHERE s2.SiteId is Null;
quit;

或者,使用CREATE TABLE ... AS语法,并根据查询确定列类型:

proc sql;
    CREATE TABLE output as
    SELECT distinct s1.subject, s2.SiteId, s2.ReferenceId 
    FROM Excel_Sheet1 as s1 
    FULL OUTER JOIN Excel_Sheet2 as s2 
    ON s1.subject = s2.subject
    WHERE s2.SiteId is Null;
quit;

以及IS NULL的替代方法:

WHERE s2.SiteId is missing;

WHERE s2.SiteId = .;

数据

data Excel_Sheet1;
    infile datalines delimiter=',' DSD; 
    length subject $ 3;
    length T_F $ 1;
    input subject $ T_F $;
    datalines;
001,T
002,F
;

data Excel_Sheet2;
    infile datalines delimiter=',' DSD; 
    length subject $ 3;
    input subject $ SiteId ReferenceId;
    datalines;
001,,55
002,44,66
;

输出

proc print data = Output; run;

Obs    subject     Id       Id;
1       001        .        55;

现在,如果要输出宏变量(而不是表),请使用INTO clause

proc sql;
    SELECT distinct s2.ReferenceId 
    INTO :output
    FROM Excel_Sheet1 as s1 
    FULL OUTER JOIN Excel_Sheet2 as s2 
    ON s1.subject = s2.subject
    WHERE s2.SiteId = .;   *is missing;
quit;

%put &output.;  
55

答案 1 :(得分:0)

更改  s1.SiteId为空 通过  s2.SiteId为空