hi,
I created tables in a do loop (e.g. Table_1 to Table_30 ) and I would like to keep only one, let's say Table_13. How can I delete the other tables in a simple way?
thank you 🙂
Hi,
If you wanted to make this a little more automated you could use the following:
data table_1 table_2 table_3 table_4 table_5 table_6;
x=1;
run;
%let keep_table = TABLE_3;
proc sql noprint;
select memname into :drop_tables separated by " "
from sashelp.vcolumn
where libname = "WORK" and
memname like "TABLE_%" and
memname ^= "&keep_table";
quit;
proc datasets;
delete &drop_tables;
quit;
This way it doesn't matter how many datasets you create, you only need to specify the single table you want to keep (and if you have a rule for which table you want to keep you can use this to define the "keep_table" macro variable).
It might be possible to adjust the program so it only creates the data sets you need. But assuming that is either not possible or not desirable, use PROC DATASETS. This is untested, but should be easily fixable if need be:
proc datasets library=work;
delete table_1 - table_12;
delete table_14 - table_30;
quit;
Why have you create 30 tables in the first place? If you only want one dataset at the end, create only the one dataset, otherwise its just wasting resources and disk space.
Also, need to state the logic for why _13 should remain.
Like this?
proc datasets lib=WORK;
delete TAB1-TAB12 TAB14-TAB30;
quit;
Hi,
If you wanted to make this a little more automated you could use the following:
data table_1 table_2 table_3 table_4 table_5 table_6;
x=1;
run;
%let keep_table = TABLE_3;
proc sql noprint;
select memname into :drop_tables separated by " "
from sashelp.vcolumn
where libname = "WORK" and
memname like "TABLE_%" and
memname ^= "&keep_table";
quit;
proc datasets;
delete &drop_tables;
quit;
This way it doesn't matter how many datasets you create, you only need to specify the single table you want to keep (and if you have a rule for which table you want to keep you can use this to define the "keep_table" macro variable).
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.