【问题标题】:Creating tables from a master table based on a condition in SAS根据 SAS 中的条件从主表创建表
【发布时间】:2015-11-17 17:41:26
【问题描述】:

数据如下:

ID Var1 Var2

A 1 2

A 3 4

B 5 6

B 7 8

C 9 10

11 月 12 日

我正在尝试从 SAS 中的上述主表创建子表,以便每个子表都包含主表中的所有记录,其中 ID 等于每个不同的值。因此,在这个特定的示例中,我想动态创建 4 个不同的表 Tab_A、Tab_B、Tab_C、Tab_D,其中 Tab_A 包含 ID=A 的主表中的所有记录,依此类推。要创建的表总数=不同的 ID 值。

【问题讨论】:

  • 我觉得this的问题会很有用

标签: sas sas-macro proc-sql


【解决方案1】:

这是创建宏的好例子。 SAS 宏以动态方式编写 SAS 代码。

此宏读取不同的 ID 值,然后生成数据步骤以执行子集。它使用mprint 选项,因此您可以查看它创建的代码。

data have;
input ID $ Var1 Var2;
datalines;
A 1 2
A 3 4
B 5 6
B 7 8
C 9 10
D 11 12
;
run;

%macro subset();
proc sql noprint;
select count(distinct id)
    into :nIDs TRIMMED
    from have;

select distinct ID
    into :ID1 - :ID&nIDs
    from have;
quit;

data 
%do i=1 %to &nIDs;
    tab_&&id&i
%end;
;
set have;

select (ID);
    %do i=1 %to &nIDs;
        when ("&&id&i") output tab_&&id&i;
    %end;
    otherwise put "PROBLEM";
end;
run;
%mend;

options mprint;
%subset;

这是显示实际执行子集的 SAS 数据步骤的日志。

MPRINT(SUBSET):   data tab_A tab_B tab_C tab_D ;
MPRINT(SUBSET):   set have;
MPRINT(SUBSET):   select (ID);
MPRINT(SUBSET):   when ("A") output tab_A;
MPRINT(SUBSET):   when ("B") output tab_B;
MPRINT(SUBSET):   when ("C") output tab_C;
MPRINT(SUBSET):   when ("D") output tab_D;
MPRINT(SUBSET):   otherwise put "PROBLEM";
MPRINT(SUBSET):   end;
MPRINT(SUBSET):   run;

【讨论】:

  • 没问题。如果这解决了您的问题,请将此答案标记为已接受并投票。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多