SAS如何设置数据步骤以仅附加现有数据?

SAS如何设置数据步骤以仅附加现有数据?,sas,Sas,我正在写一个宏函数。如果满足某些条件,将创建一些表。我想在末尾将所有这些表附加在一起。有没有一种方法可以让函数只附加现有表 我能想到的方法之一是让系统检查表是否存在,如果存在,则逐个追加。 例如: %if %sysfunc(exist(table1)) %then %do; data final_table; set table1; run; %end; %if %sysfunc(exist(table2)) %then %do;

我正在写一个宏函数。如果满足某些条件,将创建一些表。我想在末尾将所有这些表附加在一起。有没有一种方法可以让函数只附加现有表

我能想到的方法之一是让系统检查表是否存在,如果存在,则逐个追加。 例如:

    %if %sysfunc(exist(table1)) %then %do; 
           data final_table; set table1; run;
    %end;
    %if %sysfunc(exist(table2)) %then %do; 
           data final_table; set table2; run;
    etc......

这会使代码变得很长,而且没有效果。有什么办法解决这个问题吗?谢谢

不要等到最后才合并它们。边走边建

您可以使用PROC APPEND。如果基表不存在,它将创建它。否则,附加观察结果

proc append force base=final_table data=table&i;
run;
         data table1 table2....; stop run; /*create null tables*/

         /******/
         run the rest of the function
         /*****/

         data final_table; set table1 table2....; run; 
         /*it won't affect anything even if one of the table table&i is not created*/

请注意,这将要求数据集具有相同的列。

我想出了一个解决方案。如果有人想知道,我就在这里发帖。 我可以在运行这些表之前创建空表。当我追加所有中间表时,如果它们不存在,则只追加空表

proc append force base=final_table data=table&i;
run;
         data table1 table2....; stop run; /*create null tables*/

         /******/
         run the rest of the function
         /*****/

         data final_table; set table1 table2....; run; 
         /*it won't affect anything even if one of the table table&i is not created*/

对表使用命名约定,并将其存储在单个位置。 这就像使用一致的前缀一样简单,例如_TEMP_Table1、_TEMP_table2等等

然后,在附加它们时,只需使用冒号运算符:

data want;
  set _temp_table: ;
run;
仅包含以该前缀开头的表


这也使得以后可以更轻松地删除它们来清理流程。

谢谢您的回答!但是如果表&i不存在,proc append仍然不起作用。为什么要尝试追加一个没有创建的表?为什么不使用通配符<代码>设置表: