BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Hello_there
Lapis Lazuli | Level 10

For example, if we use the following data sets:

 

data practice;
      input name $ fav_color $ no.;
datalines;
mark blue 3
lisa red 9
laquisha orange 2
shauna blue 8
ray purple 1
;
run;

data practice1;
   set practice;

if fav_color="pink";
run;

data _empty;
infile datalines dsd dlm ",";
  input name $ fav_color $ no.;
datalines;
 , There is no one with this fav_color,
;
run;

Since i try to return back a data set with people who only liked "pink", and no one existed in the data set, there are 0 observations.

 

How do i have it so that i could use either some kind of code with complicated logic or a macro to be able to return that says effectively that

if this dataset has 0 observations, please return back the work._empty data set?

 

Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

@s_lassen wrote:

That's not so hard:

data want;                   
  if nobs=0 then set _empty; 
  set practice1 nobs=nobs;   
run;

It looks kind of strange, as the NOBS variable is referred before the PRACTICE1 dataset is read, but it works (NOBS is set at compile time). If you swap the two statements, it will not work, because the datastep then terminates at SET PRACTICE1.


That won't work.  When PRACTICE1 is empty the first iteration will still read past the end of PRACTICE1 so no observations will be written.

 

Perhaps you meant to include an ELSE?

data want;                   
  if nobs=0 then set _empty; 
  else set practice1 nobs=nobs;   
run;

View solution in original post

6 REPLIES 6
Reeza
Super User

Example 13: Print Information to the Output Window If a Data Set Is Empty

https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/mcrolref/p011imau3tm4jen1us2a45cyenz9.htm

 


@Hello_there wrote:

For example, if we use the following data sets:

 

data practice;
      input name $ fav_color $ no.;
datalines;
mark blue 3
lisa red 9
laquisha orange 2
shauna blue 8
ray purple 1
;
run;

data practice1;
   set practice;

if fav_color="pink";
run;

data _empty;
infile datalines dsd dlm ",";
  input name $ fav_color $ no.;
datalines;
 , There is no one with this fav_color,
;
run;

Since i try to return back a data set with people who only liked "pink", and no one existed in the data set, there are 0 observations.

 

How do i have it so that i could use either some kind of code with complicated logic or a macro to be able to return that says effectively that

if this dataset has 0 observations, please return back the work._empty data set?

 

Thanks


 

s_lassen
Meteorite | Level 14

That's not so hard:

data want;                   
  if nobs=0 then set _empty; 
  set practice1 nobs=nobs;   
run;

It looks kind of strange, as the NOBS variable is referred before the PRACTICE1 dataset is read, but it works (NOBS is set at compile time). If you swap the two statements, it will not work, because the datastep then terminates at SET PRACTICE1.

Hello_there
Lapis Lazuli | Level 10
data practice;
      input name $ fav_color $ no;
datalines;
mark blue 3
lisa red 9
laquisha orange 2
shauna blue 8
ray purple 1
;
run;

data practice1;
   set practice;

if fav_color="pink";
run;

data _empty;
retain name fav_color no1;
length name $50 fav_color $50;

infile datalines dsd dlm=",";

no1=put(no, best.);

if no1 eq . then no1= " ";

drop no;

rename no1=no;

  input name $ fav_color $ no;
datalines;
 , There is no one with this fav_color,
;
run;


/****solution*****/

data want;                   
  if nobs=0 then set _empty; 
  set practice1 nobs=nobs;   
run;

Thanks for replying!

 

Unfortunately it did not work for me.

 

In this example, practice1 has 0 obs, and I'm looking for the logic that says return back work._empty bc practice 1 had 0 obs. 

 

When I ran your code, it returned back an empty data set. 

 

 

Tom
Super User Tom
Super User

@s_lassen wrote:

That's not so hard:

data want;                   
  if nobs=0 then set _empty; 
  set practice1 nobs=nobs;   
run;

It looks kind of strange, as the NOBS variable is referred before the PRACTICE1 dataset is read, but it works (NOBS is set at compile time). If you swap the two statements, it will not work, because the datastep then terminates at SET PRACTICE1.


That won't work.  When PRACTICE1 is empty the first iteration will still read past the end of PRACTICE1 so no observations will be written.

 

Perhaps you meant to include an ELSE?

data want;                   
  if nobs=0 then set _empty; 
  else set practice1 nobs=nobs;   
run;
Hello_there
Lapis Lazuli | Level 10
Thanks, s_lassen, for your help! I got it working because of your code.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 784 views
  • 5 likes
  • 4 in conversation