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 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;
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
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.
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.
@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;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.