DATA Step, Macro, Functions and more

Delete iterated Table

Accepted Solution Solved
Reply
Contributor
Posts: 30
Accepted Solution

Delete iterated Table

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 Smiley Happy


Accepted Solutions
Solution
‎07-11-2017 04:11 AM
Occasional Contributor
Posts: 7

Re: Delete iterated Table

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).

 

View solution in original post


All Replies
Super User
Posts: 5,099

Re: Delete iterated Table

[ Edited ]

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;

Super User
Super User
Posts: 7,430

Re: Delete iterated Table

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.  

PROC Star
Posts: 1,570

Re: Delete iterated Table

Like this?

proc datasets lib=WORK;

  delete TAB1-TAB12 TAB14-TAB30;

quit;

  

Solution
‎07-11-2017 04:11 AM
Occasional Contributor
Posts: 7

Re: Delete iterated Table

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).

 

Contributor
Posts: 30

Re: Delete iterated Table

Sorry for late response. It's exactly what i wanted to do. It does the job perfectly.
I thought it was possible to exclude a vairable like this : var1-var10 except var3.
@RW9
I need to create the table before to retrieve the statistic from fastclus procedure. Then based on the statistic parameters, I only choose one.

Thank you guys for your quick answers. Have a good day.
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 132 views
  • 0 likes
  • 5 in conversation