BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Ronein
Onyx | Level 15

Hello

What is the way to delete a data set if number of rows=0 ?

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

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

 

 

View solution in original post

7 REPLIES 7
Kurt_Bremser
Super User

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.

novinosrin
Tourmaline | Level 20

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

 

 

Ronein
Onyx | Level 15

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;

 

 

r_behata
Barite | Level 11
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
Ronein
Onyx | Level 15

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?  

Kurt_Bremser
Super User

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

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

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!

Register Now

How to Concatenate Values

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 7 replies
  • 2840 views
  • 4 likes
  • 4 in conversation