PROC SQL 如何连接两个没有空单元格且加上条件的表?

问题描述 投票:0回答:2

我正在 SAS 中学习 SQL,我被要求连接两个表且不丢失任何信息,并在连接语句中包含一个条件。我已经尝试了几种类型的连接,而 Full Join 最终成为我需要的最接近的结果。但是,我无法弄清楚如何删除具有空值的行,也不知道如何只保留我需要的会话。我只能使用 PROC SQL。

虚构数据:

PROC SQL;
    CREATE TABLE Students_SCORE AS
    SELECT *
    FROM TABLE_A AS A
        FULL JOIN (SELECT *
        FROM TABLE_B
        WHERE SESSION_ID HAVING ('2021' AND '2022')) AS B
            ON A.ID=B_ID 
            AND A.NAME=B_NAME;
QUIT;

收到的表格总结了所有行,但在任一侧显示空单元格(空的 A.rows 和 filled.B.rows 或 filled.A.rows with empty.B.rows)。 请注意,该示例是虚构的,因此,如果有一种方法可以不提及所有列,那就太好了,因为我拥有的数据集每个表有 15 列,共有 4 列。 感谢您的帮助!

Tables

Result

sql sas proc
2个回答
0
投票

你只需要一个简单的内连接。

proc sql;
    create table want as
    select tableA.*, tableB.scoreengl, tableB.scorescien, tableB.session  
    from tableA
    inner join tableB
    on tableA.id = tableB.id
    ;
quit;
id   name  scoreMATH  scoreHIST  scoreENGL  scoreSCIEN  session
 1  David     45         59.5       77.5       60        2021
 3  Sam       45         45         65         52        2022
 4  Ram       54         60         80         75        2022
 5  Bart      87         88.5       90         98        2021
 6  Mary      92         92         95         92        2021
 8  Dane      23         50         55         45        2021
 9  Jenny     87         92         98         80        2022
10  Ken       87         60         85         88        2021

编辑以下评论。

proc sql;
    create table want as
    select tableA.*, tableB.scoreengl, tableB.scorescien, tableB.session  
    from tableA
    inner join tableB
    on tableA.id = tableB.id
    where session in ('2021', '2022')
    ;
quit;

0
投票

我能够解决这个问题:

proc sql;
    create table Want as
    select tableA.*, tableB.*  
    from tableA
    full join (select *
    from tableB
    where session between 2021 and 2022) tableB
    on tableA.id = tableB.id;
quit;
© www.soinside.com 2019 - 2024. All rights reserved.