BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sameer112217
Quartz | Level 8

Everyday a job runs and it creates a dataset with date extension in the library called final. For example date extension for the first record read this 2018 is the year, 05 is the month and 04 is the day which is yymmddn8.  date format

 

Sample_20180504

Sample_20180503

Sample_20180502

Sample_20180501

Sample_20180430

Sample_20180429

Sample_20180428

 

I want to create a macro or code which will delete the tables or dataset in the final library which has file extension more than 3 days old. In the above example all the dataset older than 2nd May 2018 extension should be deleted if I run it today which is 4th May 2018

 

I tried the code but it did not work out

data _null_;
  call symput('to_delete',catt('sample_',year(today())-0,
   put(intnx('day',today(),-3), z2.)));
run;

 

Similarly you can put for month but it only deletes one record which is less than 3 that is Sample_20180501. I want to delete all that are older than 3 days old which is till 28th april 2018 record

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

@sameer112217

Here another coding option.

/*** create sample data ***/
libname final (work);
data _null_;
  dcl hash h1(dataset:'sashelp.class');
  h1.defineKey('name');
  h1.defineData(all:'y');
  h1.defineDone();
  set sashelp.class(obs=1);
  do date=today()-5 to today();
    h1.output(dataset:cats('final.sample_',put(date,yymmddn8.)));
  end;
run;

/*** delete ds with a date parte older today()-3 ***/
/* create list of ds to be deleted */
%let del_list=;
proc sql noprint;
  select memname into :del_list separated by ' '
  from dictionary.tables
  where libname='FINAL' and memname like 'SAMPLE^_%' escape '^'
    and input(scan(memname,-1,'_'),yymmdd8.)<today()-3
  ;
quit;

/* delete ds in list. */
/* use additional dummy ds name to avoid errors in case &del_list is empty */
proc datasets lib=final nolist nowarn;
  delete &del_list _dummy_123456789;
  run;
quit;

 

View solution in original post

4 REPLIES 4
novinosrin
Tourmaline | Level 20

No need for macro, generate proc datasets statements using Call execute reading from sashelp,vtable 

-->
if today()-3<=crdate<today() then call execute

 

 

Data _null_;
set sashelp.vtable end=last;
if _n_=1 then call execute ('proc datasets lib=work;');
if  today()-3<=datepart(crdate)<today()  then call execute ("delete "||' '||memname||";");
if last then call execute('quit;');
run;
sameer112217
Quartz | Level 8
Novinosrin, it will delete dataset which is 3 days older however my job is creating a dataset with an extension

Sample_20180504 . It is 04th may, 2018.The date part is an extension that needs attention for a particular dataset name and not creation date for every dataset
art297
Opal | Level 21

I think that the following will do what you want:

data _null_;
  set sashelp.vtable (where=(libname eq 'WORK'));
  if _n_=1 then pattern_num = PRXPARSE("/_\d{8}$/");
  retain pattern_num;
  position=PRXMATCH(pattern_num,trim(memname));
  if position then do;
    if input(substr(memname,position+1),yymmdd8.) le today()-3 then do;
      forexec=catt('proc delete data=work.',memname,';run;');
      call execute(forexec);
    end;
  end;
run;

Art, CEO, AnalystFinder.com

 

Patrick
Opal | Level 21

@sameer112217

Here another coding option.

/*** create sample data ***/
libname final (work);
data _null_;
  dcl hash h1(dataset:'sashelp.class');
  h1.defineKey('name');
  h1.defineData(all:'y');
  h1.defineDone();
  set sashelp.class(obs=1);
  do date=today()-5 to today();
    h1.output(dataset:cats('final.sample_',put(date,yymmddn8.)));
  end;
run;

/*** delete ds with a date parte older today()-3 ***/
/* create list of ds to be deleted */
%let del_list=;
proc sql noprint;
  select memname into :del_list separated by ' '
  from dictionary.tables
  where libname='FINAL' and memname like 'SAMPLE^_%' escape '^'
    and input(scan(memname,-1,'_'),yymmdd8.)<today()-3
  ;
quit;

/* delete ds in list. */
/* use additional dummy ds name to avoid errors in case &del_list is empty */
proc datasets lib=final nolist nowarn;
  delete &del_list _dummy_123456789;
  run;
quit;

 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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
  • 4 replies
  • 940 views
  • 5 likes
  • 4 in conversation