Reputation: 39
I am writing a macro function. Some of the tables will be created if certain criteria is met. I want to append all these tables together at the end. Is there a way that I can let the function to append the existing table only?
One of the methods I can think of is let the system check if the table exist, if yes then append one by one. For example:
%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......
this will make the code really long and not effective. Is there any way to solve this? Thank you
Upvotes: 2
Views: 2024
Reputation: 21294
Use a naming convention for your tables and store them in a single location. This would be as simple as using a consistent prefix, such as _TEMP_Table1, _temp_table2, etc.
Then when you're appending them just use the colon operator:
data want;
set _temp_table: ;
run;
Only tables that start with that prefix will be included.
This also makes it easier to drop them all later to clean up your process.
Upvotes: 2
Reputation: 51621
Don't wait to the end to combine them. Build it up as you go.
You could use PROC APPEND. If the BASE table doesn't exist then it will create it. Otherwise the observations are appended.
proc append force base=final_table data=table&i;
run;
Note that this will require that the datasets have the same columns.
Upvotes: 2
Reputation: 39
I have come up with a solution. I post here in case anyone wants to know. I can create empty tables before running any of these tables. When I append all the intermediate tables, it they are not exist only empty table will be appended.
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*/
Upvotes: -1