BookmarkSubscribeRSS Feed
ptadgerv
Obsidian | Level 7

Dear SAS community:

I would like to write a new observation when a dataset set presents 0 observations.

For example, conv1c is an empty dataset (nobs=0) which is checked with the following macro.

 

%let dsid = %sysfunc( open(conv1c) );
%global nobss;
%let nobss = %sysfunc( attrn(&dsid,nobs) );
%let rc = %sysfunc( close(&dsid) );
%put &nobss;

 

So far so good! the problem is now:

data conv1cc;
	set conv1c;
	if &nobss=0 then status=1;
 run;

A similar problem has been posted here: https://communities.sas.com/t5/SAS-Programming/How-to-test-for-zero-observations-and-still-be-in-con... (but is not quite the same)

6 REPLIES 6
gamotte
Rhodochrosite | Level 12

Hello,

 

Try

data conv1cc;
    if _N_=1 and last then do;
        status=1;
        output;
    end;
    set conv1c end=last;
    output;
run;
andreas_lds
Jade | Level 19

You don't need to check the number of obs with, just use nobs-option:

data work.EmptyClass;
   set sashelp.class;
   where Age < 10;

   keep Name;
run;

data NewClass;
   if 0 then set work.EmptyClass;

   if _nobs = 0 then do;
      Name = 'Alex';
      output;
   end;
   set work.EmptyClass nobs=_nobs;
   output;
run;
PeterClemmensen
Tourmaline | Level 20

If I understand you correct, you do not need a macro.

 

data have;
   set sashelp.class;
   stop;
run;

data want;
   if n = 0 then output;
   set have nobs=n;
run;
ballardw
Super User

@ptadgerv wrote:

Dear SAS community:

I would like to write a new observation when a dataset set presents 0 observations.

For example, conv1c is an empty dataset (nobs=0) which is checked with the following macro.

 

%let dsid = %sysfunc( open(conv1c) );
%global nobss;
%let nobss = %sysfunc( attrn(&dsid,nobs) );
%let rc = %sysfunc( close(&dsid) );
%put &nobss;

 

So far so good! the problem is now:

data conv1cc;
	set conv1c;
	if &nobss=0 then status=1;
 run;

A similar problem has been posted here: https://communities.sas.com/t5/SAS-Programming/How-to-test-for-zero-observations-and-still-be-in-con... (but is not quite the same)


I think you may be looking for something similar to

%let dsid = %sysfunc( open(conv1c) );
%global nobss;
%let nobss = %sysfunc( attrn(&dsid,nobs) );
%let rc = %sysfunc( close(&dsid) );
%put &nobss;
%if &nobss = 0 %then %do;
   data conv1cc;
	   set conv1c;
	   status=1;
          <or what ever you mean by New observation>
   run;
%end;
%else %do;
   <what ever is appropriate when the 
    count is > 0 
   >
%end;

Since you are working with macro language then use the test of the MACRO variable to decide what code to generate.

Ksharp
Super User
data conv1c;
 set sashelp.class;
 stop;
run;




%let dsid = %sysfunc( open(conv1c) );
%global nobss;
%let nobss = %sysfunc( attrn(&dsid,nlobs) );
%let rc = %sysfunc( close(&dsid) );
%put &nobss;

%macro xx;
%if &nobss=0 %then %do;
data conv1cc;
	   status=1;output;
	   set conv1c;
   run;
%end;
%mend;


%xx 
ptadgerv
Obsidian | Level 7

Thanks for so many solutions!

Now, the problem is to choose the best one 😄

I will require some time to do it (please be patient, too many deadlines this week 😞   ) 

Today, in my problem..., I combined several of the proposed solutions in one approach. That's why is difficult to choose just one solution (but I'll do it).

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 6 replies
  • 4991 views
  • 6 likes
  • 6 in conversation