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

I've to test for number of observations in the WANT dataset as per the code (similar) below and I want to output the data to nobs_test dataset only if there is any observation in the WANT dataset. If there is no observation in the source dataset 'WANT' then the code should not create nobs_test dataset.

 

data nobs_test;
set want;
if nobs gt 0 then output; /*Need code to test for obs in WANT dataset*/
FLT_Id=&f_id.;
run;
1 ACCEPTED SOLUTION

Accepted Solutions
ed_sas_member
Meteorite | Level 14

Hi @David_Billa 

 

You can try this code. Hope this help! 

 

proc sql noprint;
	select count(*) into: obsnumber from want; /* Retrieve the number of observations in the dataset */
quit;

%if &obsnumber > 0 %then %do; /* Test if there are observations in the dataset and process the following data step conditionally to the result */

data nobs_test;
	set want;
	FLT_Id=&f_id.;
run;

%end;

Best,

View solution in original post

2 REPLIES 2
ed_sas_member
Meteorite | Level 14

Hi @David_Billa 

 

You can try this code. Hope this help! 

 

proc sql noprint;
	select count(*) into: obsnumber from want; /* Retrieve the number of observations in the dataset */
quit;

%if &obsnumber > 0 %then %do; /* Test if there are observations in the dataset and process the following data step conditionally to the result */

data nobs_test;
	set want;
	FLT_Id=&f_id.;
run;

%end;

Best,

gamotte
Rhodochrosite | Level 12

Hello,

 

In the following program, the call execute instruction is executed only if the dataset in the preceding set instruction

contains data.

 

data want1;
    x=1; output;
    x=3; output;
run;

data want2;
    set want1;
    stop;
run;

%macro create_ds(in, out);

    data _NULL_;
        set &in.;
        call execute("data &out.; set &in.; FLT_Id=&f_id.; run;");
        stop;
    run;

%mend;

%let f_id=x;

%create_ds(want1, nobs_test1);
%create_ds(want2, nobs_test2);

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 534 views
  • 2 likes
  • 3 in conversation