How do I delete all datasets in a library except those that contain, say, _MEANS2, in the dataset name ?
Thanks...
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.
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
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.
Could we see your log?
UPCASE
Yep! Most likely! Also make sure there is a blank in separated by ' '.
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);
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!
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!
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.
Ready to level-up your skills? Choose your own adventure.