Can you please tell me where I missed something? I need to run a bunch of macros that keep creating 9 temporary datasets, then delete the empty one before going to the next step of the calculations.
I tried your macro after I created the temp files. It checks how many observations but does not delete the empty ones.
Thanks!
%macro apaga;
%let EMPTY=1;
%do i=1 %to 9;
data _null_;
set x&i;
call symput('EMPTY','0'); /* this will only occur if &DATA is not empty */
run;
%end;
/*
Now, you just need to check the macro var for its value at the end, to perform (or not) a proc dataset delete,
*/
%if &EMPTY %then %do;
proc dataset lib=work nolist;
delete work.x&i; /* delete dataset */
quit;
%end;
%mend;
%apaga; run;
So as soon as you find one non-empty dataset you will never find any more empty datasets because EMPTY will never be set back to true.
Move the %LET statement inside the %DO loop.
same result with:
%macro apaga;
%do i=1 %to 9;
%let EMPTY=1;
data _null_;
set x&i;
call symput('EMPTY','0'); /* this will only occur if &DATA is not empty */
run;
%end;
/*
Now, you just need to check the macro var for its value at the end, to perform (or not) a proc dataset delete,*/
%if &EMPTY %then %do;
proc datasets lib=work nolist;
delete work.x&i;
%end;/*%end;***also tried put it all inside teh first 2 loops and the result was the same***/
quit;
%mend;
%apaga; run;
How about trying this?
%if &EMPTY = 1 %then %do;
proc dataset lib=work nolist;
delete work.x&i; /* delete dataset */
quit;
%end;
same result. If the variable x is binary, if x means the same as if x=1.
This works for me:
data class;
set sashelp.class (obs=0);
run;
%macro delete_table;
%let EMPTY=1;
data _null_;
set class;
call symput('EMPTY','0');
run;
%put empty = ∅
%if &EMPTY %then %do;
proc datasets lib=work nolist;
delete class;
run;
quit;
%end;
%mend delete_table;
%delete_table;
Here's a variation that simplifies the logic, eliminating the complications.
%macro apaga;
%do i=1 %to 9;
data _null_;
if done then call execute("proc delete data=x&i; run;");
stop;
set x&i end=done;
run;
%end;
%mend;
%apaga
By switching to PROC DELETE, the code also can be easier to use if your source data sets are in more than one library.
one more:
data test;
set sashelp.class (obs=0);
run;
%macro delData(ds);
data _null_;
if nobs=0 then call execute("proc delete data=&ds.; run;");
stop;
set &ds. nobs=nobs;
run;
%mend delData;
%delData(test)
🙂
Bart
Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.
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.
Ready to level-up your skills? Choose your own adventure.