有没有办法在遍历名称时跳过丢失的数据集?

Is there a way to skip missing data sets when iterating through names?

我在 SAS 中有一些数据集,我试图将它们整理成一个更大的数据集,稍后我将对其进行过滤。它们都被称为 table_201802。我的问题是缺少几个月(即存在 table201802table201804 及以上,但不存在 table201803.

我对 SAS 还很陌生,但到目前为止我尝试的是创建一个名为 output testing 的新数据集和 运行 一个遍历名称的宏循环(他们去从 201802201903,它们是月度数据,因此从 812 到 900 的任何数据都不存在)。

data output_testing;
set
%do i=802 %to 812;
    LIBRARY.table_201&i
%end;
;
run;
%mend append;

我希望代码忽略缺失的 table,只查找确实存在的,然后将它们附加到新的 output_testing table.

一些带有 proc append 的宏代码应该可以解决问题。


%let n = 10;
%macro get_list_table;

%do i = 1 %to &n;
    %let dsn = data&n;
    %if %sysfunc(exist(&dsn)) %then %do;
        proc append data = &dsn base = appended_data  force;
        run;

    %end;
%end;
%mend;

您可以使用快捷方式:

data output_testing;
set LIBRARY.table_201:
;
run;

但在这种情况下,您将设置所有以 "table_201" 开头的表。 例如:

LIBRARY.table_201tablesss LIBRARY.table_201ed56

如果 table 名称前缀不同,并且您确信 table 之间的数据结构是一致的(变量名称、类型和长度相同),那么 table 可以使用 table 名称前缀列表堆叠 (:)

对于特定已知范围的 table 个名称,您还可以使用编号范围列表 (-) 选项卡

data have190101 have190102 have190103;
  x =1;
run;

data want_version1_stack; /* any table name that starts with have */
  set have:;
run;

data want_version1b_stack; /* 2019 and 2020 */
  set have19: have20:;
run;

options nodsnferr;
data want_version2_stack; /* any table names in the iterated numeric range */
  set have190101-have191231;
run;
options dsnferr;

来自helps

Using Data Set Lists with SET
You can use data set lists with the SET statement. Data set lists provide a quick way to reference existing groups of data sets. These data set lists must either be name prefix lists or numbered range lists.

Name prefix lists refer to all data sets that begin with a specified character string. For example, set SALES1:; tells SAS to read all data sets that start with "SALES1" such as SALES1, SALES10, SALES11, and SALES12. >

Numbered range lists require you to have a series of data sets with the same name, except for the last character or characters, which are consecutive numbers. In a numbered range list, you can begin with any number and end with any number. For example, these lists refer to the same data sets:

  • sales1 sales2 sales3 sales4
  • sales1-sales4