BookmarkSubscribeRSS Feed
David_Billa
Rhodochrosite | Level 12

In the the following code, I would like to write a data to the dataset 'oper_flt' only if there is a Observation.

 

proc sql;
create table temp as select * from &table_name
 where &where ;
run;

data oper_flt;
set temp;
/*Need a condition to check for Observation and insert a data only if there is a Observation otherwise do not insert any data*/
run;
2 REPLIES 2
gamotte
Rhodochrosite | Level 12

hello,

 

The set instruction will exit the data step whenever there is no further observation to read.

So, if your dataset is empty, every instruction after the set instruction will be ignored

and the output dataset will also be empty.

 

21   proc sql noprint;
22   CREATE TABLE temp AS
23   SELECT * FROM sashelp.class
24   WHERE AGE<0;
NOTE: Table WORK.TEMP created, with 0 rows and 5 columns.

25   quit;
NOTE: PROCEDURE SQL used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


26
27   data want;
28   set temp end=eof;
29   output;
30   if eof then do;
31       AGE=10;
32       output;
33   end;
34   run;

NOTE: There were 0 observations read from the data set WORK.TEMP.
NOTE: The data set WORK.WANT has 0 observations and 5 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
ed_sas_member
Meteorite | Level 14

Hi @David_Billa ,

 

Maybe you can try this :

 

Hope this help

 

proc sql;
	create table temp as select * from &table_name where &where;
quit;

/* -> The macrovariable sqlobs is automatically created and contains the number of observations */

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

data oper_flt;
	set temp;
run;

%end;

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
  • 2 replies
  • 809 views
  • 0 likes
  • 3 in conversation