【问题标题】:how to keep a column from a specific data set using data step in sas如何使用 sas 中的数据步骤保留特定数据集中的列
【发布时间】:2018-03-26 10:53:53
【问题描述】:

我有 2 个表,它们具有相同属性的同一列。我想从两个表中选择该列和其他列。例子

table_1

ID | column_1 | column_2
 1 |  col_1   |  col_2
 2 |  col_1   |  col_2
 3 |  col_1   |  col_2


table_2
ID | column_3 | column_4
 4 |  col_3   |  col_4
 5 |  col_3   |  col_4
 6 |  col_3   |  col_4

我想创建一个表格

Required

ID | column_1 | column_4 
 1 |  col_1   |  col_4   
 2 |  col_1   |  col_4   
 3 |  col_1   |  col_4   

我想用数据步骤来做

data required;
set table_1 table_2;
keep ID column_1 column_4;
run;

但它给了我 6 行。

我可以使用 proc sql 获取我的表

proc sql noprint;
create table required as
select t1.Id, t1.column_1, t2.column_4
from table_1 as t1, table_2 as t2;
quit;

我希望对数据步骤做同样的事情

【问题讨论】:

  • 你的加盟条件是什么?是否要将表 1 中具有 id = n 的行与表 2 中具有 id = n + 3 的行进行匹配?还是要完全忽略id

标签: sql sas


【解决方案1】:

如果您使用SET,则按顺序读取数据集。如果您想逐条记录组合数据集,请改用MERGE。通常您会使用BY 语句根据一些关键变量组合记录。但是,如果您不使用 BY 语句,那么 SAS 将按顺序合并记录。

还要注意变量名冲突。您的两个输入都有一个 ID 变量。看起来您只想保留第一个数据集中的那个。您可以使用 KEEP= 数据集选项告诉 SAS 要从数据集中包含哪些变量。

data required;
   merge table_1 (keep=id column_1) table_2 (keep=column_4);
run;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-19
    • 1970-01-01
    • 1970-01-01
    • 2018-05-31
    • 2016-11-17
    • 2019-06-26
    相关资源
    最近更新 更多