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;

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

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
  • 901 views
  • 0 likes
  • 3 in conversation