DATA Step, Macro, Functions and more

dataset name pattern and the EXIST function

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 91
Accepted Solution

dataset name pattern and the EXIST function

[ Edited ]

SAS Folks-

 

Does anyone know a shortcut for dataset names with the same pattern in the EXIST function?

 

I want to clear all datasets in the WORK library with the filename pattern MASTER before I start a macro to read external files, and I use : to get them all.

 

 

     proc datasets library=WORK ;
       delete MASTER: ;
       run ;
       quit ;

 

 

 

However, if there are no existing MASTER: datasets, it generates a note.

 

NOTE: The file WORK.MASTER: (memtype=DATA) was not found, but appears on a DELETE statement.

 

 

Just for neatness, I would like to test to here are no existing MASTER: datasets before I do the deletion, maybe something along this line.

 

%macro DEL_MASTER(NAME);
    %if %sysfunc(exist(&name)) %then %do ;
         proc datasets library=WORK ;
           delete MASTER: ;
           run ;
           quit ;
    %end ;
%mend ;

%DEL_MASTER(MASTER_somedataset);

 

 

The only drawback is that it requires that I know at least one of the MASTER dataset names.

 

I use MASTER datasets in varying forms quite frequently when reading external data, and would love to be able to set up a macro to do this deletion without having to know any of the actual MASTER filenames.

 

%DEL_MASTER(MASTER:);

 

 

does not work. Does anyone know a shortcut that does? 

 

If not, I could use the alternative of SASHELP.VMEMBER on LIBNAME and MEMNAME.

 

We are running 9.4 (TS1M3)  on a 64-bit Windows 7 virtual server by remote login.

 

Thanks for your help!

 

Wendy T


Accepted Solutions
Solution
‎11-08-2016 12:35 PM
Super User
Super User
Posts: 7,392

Re: dataset name pattern and the EXIST function

Hi,

 

data _null_;
  set sashelp.vtable (where=(libname="WORK" and index(memname,"MASTER") > 0));
  if _n_=1 then call execute('proc datasets library=WORK; delete MASTER:; run; quit;');
run;

Only if a dataset master exists in work then the proc datasets code will get generated.  TBH though, is it a good idea to have master datasets in a tempoprary area?  Having to remove datasets - especially in macros - seems to me to show the process is not very good.  Why do you need to delete them?  Why does the import of new data not overwite?  What is your import process?  I would suggest a sound import process would principally be responsible for importing data to a fixed area and validating it.  Any other process can then just assume the data is there and ready.

View solution in original post


All Replies
Solution
‎11-08-2016 12:35 PM
Super User
Super User
Posts: 7,392

Re: dataset name pattern and the EXIST function

Hi,

 

data _null_;
  set sashelp.vtable (where=(libname="WORK" and index(memname,"MASTER") > 0));
  if _n_=1 then call execute('proc datasets library=WORK; delete MASTER:; run; quit;');
run;

Only if a dataset master exists in work then the proc datasets code will get generated.  TBH though, is it a good idea to have master datasets in a tempoprary area?  Having to remove datasets - especially in macros - seems to me to show the process is not very good.  Why do you need to delete them?  Why does the import of new data not overwite?  What is your import process?  I would suggest a sound import process would principally be responsible for importing data to a fixed area and validating it.  Any other process can then just assume the data is there and ready.

Frequent Contributor
Posts: 91

Re: dataset name pattern and the EXIST function

RW9-

 

Thanks for the call execute method - that didn't even cross my mind. 

 

Just FYI... 

The typical scenario I use for WORK.MASTER: is to put together a bunch of files into a SAS dataset (usually badly formatted Excel files).  The name came from a project that I did with a colleague, and when the process changed, the name did not (my bad).

 

Once I have MASTER, then I proceed with whatever needs to be done, and save the dataset to a permanent library. At that point, the MASTER: dataset(s) can be deleted to keep everything tidy.

 

I also like to run the delete of MASTER before starting, just to make sure there aren't any leftovers from a previous process.

 

Thanks so much for your help!

 

Wendy T

Respected Advisor
Posts: 3,777

Re: dataset name pattern and the EXIST function

Why not use the NOWARN proc datasets statement option.

 

proc datasets nowarn
Frequent Contributor
Posts: 91

Re: dataset name pattern and the EXIST function

Data _null_-

 

I thought about NOWARN, but I didn't want to turn off warnings in case there was another issue.

 

Thanks so much!

 

Wendy 

☑ This topic is SOLVED.

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

Discussion stats
  • 4 replies
  • 346 views
  • 0 likes
  • 3 in conversation