Hello
What is the way to delete a data set if number of rows=0 ?
Hi @Ronein Here is a demo
/*Create a sample with 0 rows*/
data w;
stop;
set sashelp.class;
run;
/*Solution*/
%macro t;
data _null_;
call symputx('n',n);
stop;
set w nobs=n;
run;
%put &n;
%if &n=0 %then %do;
proc delete data=w;
run;
%end;
%mend t;
%t
The number of observations is stored in column nobs in dictionary.tables (SQL).
Once you have a dataset with dataset names, you can use call execute from that dataset to create a proc delete step.
Hi @Ronein Here is a demo
/*Create a sample with 0 rows*/
data w;
stop;
set sashelp.class;
run;
/*Solution*/
%macro t;
data _null_;
call symputx('n',n);
stop;
set w nobs=n;
run;
%put &n;
%if &n=0 %then %do;
proc delete data=w;
run;
%end;
%mend t;
%t
May you please explain the need of "STOP" in your code?
I know that nobs is automatic variable that contains number of rows in data set names in set statement.
I understand that in code nobs=n we place the value of nobs in variable n.
I also understand that using call symputx we put the value of variable n in a macro variable called also n.
Why do we need to use "Stop"?
data _null_;
call symputx('n',n);
stop;
set w nobs=n;
run;
Without stop, the data step would read the whole dataset, which is not necessary and would be a waste of time and resources.
data h;
set sashelp.class;
stop;
run;
%let id=%sysfunc(open(h));
%let nobs=%sysfunc(attrn(&id,nobs));
%let cl=%sysfunc(close(&id));
%put &=nobs;
%macro chck;
%if &nobs eq 0 %then %do;
proc delete data=h;
run;
%end;
%mend;
%chck
Thank you.
In the macro that you create I didn't see using of the macro variables "ID" and "cl".
May you explain please why did you create them and where did you use them in order to delete data set with 0 observations?
@Ronein wrote:
Thank you.
In the macro that you create I didn't see using of the macro variables "ID" and "cl".
May you explain please why did you create them and where did you use them in order to delete data set with 0 observations?
id is used in the attrn function, cl is needed to receive the return value of the close function. It has no other use.
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.