I have a few data sets in SAS which I am trying to collate into one larger set which I will be filtering later. They're all called something like table_201802
. My problem is that there are a few missing months (i.e. there exists table201802
and table201804
and up, but not table201803
.
I'm new enough to SAS, but what I've tried so far is to create a new data set called output testing
and ran a macro loop iterating over the names (they go from 201802
to 201903
, and they're monthly data so anything from 812 to 900 won't exist).
data output_testing;
set
%do i=802 %to 812;
LIBRARY.table_201&i
%end;
;
run;
%mend append;
I want the code to ignore missing tables and just look for ones that do exist and then append them to the new output_testing
table.
If the table name prefix is distinct, and you are confident the data structures amongst the tables are consistent (variable names, types and lengths are the same) then the table can be stacked using table name prefix lists (:
)
For a specific known range of table names you can also use numbered range lists (-
) tab
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;
From 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