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!

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

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
  • 8 replies
  • 10955 views
  • 7 likes
  • 4 in conversation