BookmarkSubscribeRSS Feed
Xinxin
Obsidian | Level 7

How do I delete all datasets in a library except those that contain, say, _MEANS2, in the dataset name ?

Thanks...

8 REPLIES 8
data_null__
Jade | Level 19

If they started with _MEANS2 it would be really easy.

proc datasets library=... memtype=data;

   save _means2:;

   run;

   quit;

For contains you will need to build the SAVE list from meta data DICTIONARY.MEMBERS perhaps.  Use SQL INTO macro variable and use macro variable in SAVE statement.  Easy enough.

Haikuo
Onyx | Level 15

Thanks, Data_null_. And here is the code following his instruction:

proc sql;

select DISTINCT memname INTO :NAMES SEPARATED BY ' '

  from dictionary.columns where libname='WORK' AND memname ? '_means2'

  ;

  QUIT;

proc datasets library=WORK memtype=data;

   save &NAMES;

   run;

   quit;

Note: I have use work library as an example.

Regards,

Haikuo

Xinxin
Obsidian | Level 7

Thank you, Hai.kuo, for your reply. Although I had a lot of datasets in the work library with and without "_means2" in the dataset name, this code did not select anything into Names and so, in the next step the marco could not be resolved.

Haikuo
Onyx | Level 15

Yep! Most likely! Also make sure there is a blank in separated by ' '.

Augusto
Obsidian | Level 7

ANOTHER WAY

%MACRO WANT(LIB);
PROC SQL NOPRINT;
SELECT
   COUNT(*)
  ,MEMNAME INTO : CONT,  : NAME
SEPARATED BY " "
    FROM
DICTIONARY.TABLES
    WHERE
UPCASE(LIBNAME) EQ "&LIB."
    AND

MEMNAME NOT CONTAINS '_MEANS2';
  QUIT;

   %DO %UNTIL (&CONT.);
         PROC DATASETS LIB = &LIB. MEMTYPE = DATA NOLIST;
            DELETE &NAME.;
         RUN;
    %END;


%MEND;

%WANT (WORK);

Xinxin
Obsidian | Level 7

Thank you, data_null_; , Hai.kuo and Augusto for your quick and very helpful replies ! This is better than writing out the datasets in a Proc Delete and I also learnt something new. Thanks again for your help!

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 11453 views
  • 7 likes
  • 4 in conversation