BookmarkSubscribeRSS Feed
sam_sas2
Obsidian | Level 7

Hi All,

 

So I have this task of deleting around 100 datasets. But the problem is the datasets are named this way: adc_1_a, adc_2_a,......,adc_45_d, etc.

 

So I tried doing this:

proc datasets nolist lib=hmp;
delete adc_1_: - adc_45_;
run;quit;

but this throws up an error.
How do I achieve this? Thanks in advance 🙂

7 REPLIES 7
RichardDeVen
Barite | Level 11

You can use metadata table DICTIONARY.TABLES to construct a list of tables that match some naming convention detectable with a regular expression pattern.

 

Example:

Some sample data sets with the same common prefix (adc_:) but not all are adc_#_<letter>

* create some sample data sets whose names start with adc_;
data adc_1_a adc_2_a adc_45_d adc_patients; x=1; run;
%let tables=;
proc sql; reset noprint; select memname into :tables separated by ' ' from dictionary.tables where libname = 'WORK' /* sample data sets are in the WORK library */ and prxmatch ('/adc_\d+_[abcd]/i', memname) /* pattern for tables named adc_<digits>_<single letter a, b, c, or d> */ ; proc datasets nolist lib=work; delete &tables; quit; %symdel tables;
sam_sas2
Obsidian | Level 7
What's the purpose of first 3 lines?
RichardDeVen
Barite | Level 11

The first three lines creates some sample data sets for the example code to work with.

andreas_lds
Jade | Level 19

Not clear what you don't want to delete. Is deleting anything starting with adc_ acceptable? If yes, use adc_: in the delete statement. If not, please define the rules identifying a dataset that should be deleted.

sam_sas2
Obsidian | Level 7
No, so I want to delete all datasets that have a number in between. I have datasets like adc_ab which I shouldn't delete. Hope this gives more clarity.
Tom
Super User Tom
Super User

@sam_sas2 wrote:
No, so I want to delete all datasets that have a number in between. I have datasets like adc_ab which I shouldn't delete. Hope this gives more clarity.

SAS has an ANYDIGIT() function.

s_lassen
Meteorite | Level 14

You might try SQL SELECT INTO, with a Pearl Regular Expression to find the datasets you are interested in (or not, actually):

proc sql noprint;
  select memname into :ToBeDeleted separated by ' ' from dictionary.tables
  where libname='HMP' and prxmatch('/adc_\d+_./i',memname);
quit;

And then use the generated macro variable in PROC DATASETS:

proc datasets nolist lib=hmp;
delete &ToBeDeleted;
run;quit;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 598 views
  • 2 likes
  • 5 in conversation