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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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