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

Hi all!

 

I have some SAS-jobs that runs each evening that creates datasets that is used to update dashboards. These tables are appended to already existing datasets.

 

Sometimes something in these SAS-jobs goes wrong so the datasets is empty, often because there are some capacity issues in the central systems that makes some queries run empty. And if these empty datasets then are appended, then the dashboards shows nothing.

 

I would rather have a day old data in the dashboards then no data at all and hope the sas-jobs runs fine the next day. I can't find good examples that helps me with what I'm trying to achieve.

 

So how can I stop the appending of empty datasets, but also let the code below this appending continue to run?

 

Something like:

 

if obs > 0 in new_dataset then do;

proc sql; delete from Server.old_dataset; quit;

proc append base=Server.old_dataset data=new_dataset;
run;

end;

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Have your SAS admins update to the current maintenance level 3 days before yesterday. There are so many goodies in there (including the use of %IF %THEN %DO %END in "open code") that not updating (which only takes some work, updates are included in your license) is simply dumb.

 

Having said that, you need to wrap it into a macro for the time being:

%macro rest_of_code;
%if &nobs. gt 0
%then %do;
/* remaining code */
%end;
%mend;
%rest_of_code

View solution in original post

6 REPLIES 6
Kurt_Bremser
Super User

Retrieve the obs number from DICTIONARY.TABLES, and use it in a macro condition:

%let nobs = 0; /* in case dataset does not exist at all */
proc sql noprint;
select nobs into :nobs from dictionary.tables
where libname = "WORK" and mename = "NEW_DATASET"; /* upper case here! */
quit;

%if &nobs. gt 0
%then %do;
/* remaining code */
%end;
wixol87
Fluorite | Level 6
Hi.
Thanks for answering.

I see that the first part of your code runs smoothly and I get a output in the macro. I however get the error-message below on the %IF-statement, and I see that the code I have in the "remaining code" runs whether or not the macro is 0 or greater than 0:

ERROR: The %IF statement is not valid in open code.
27
28 %if &nobs. gt 0
29 %then %do;
Kurt_Bremser
Super User

Have your SAS admins update to the current maintenance level 3 days before yesterday. There are so many goodies in there (including the use of %IF %THEN %DO %END in "open code") that not updating (which only takes some work, updates are included in your license) is simply dumb.

 

Having said that, you need to wrap it into a macro for the time being:

%macro rest_of_code;
%if &nobs. gt 0
%then %do;
/* remaining code */
%end;
%mend;
%rest_of_code
sbxkoenk
SAS Super FREQ

Hello @wixol87 ,

 

This is not responding "1-on-1" on your request, but maybe you can use "exit" functionality in your macro.

I like it and use it from time-to-time.

Example: Providing Exits in a Large Macro

The %GOTO statement is useful in large macros when you want to provide an exit if an error occurs.

%macro check(parm);
   %local status;
   %if &parm= %then %do;
       %put ERROR:  You must supply a parameter to macro CHECK.;
       %goto exit;
   %end;
   more macro statements that test for error conditions 
   %if &status > 0 %then %do;
       %put ERROR:  File is empty.;
       %goto exit;
   %end;
   more macro statements that generate text 
   %put Check completed successfully.;
%exit: %mend check;

Koen

wixol87
Fluorite | Level 6

Thanks for this. I'll have it in mind if a come across a problem where this could be useful 🙂

 

wixol87
Fluorite | Level 6

That worked like a charm!

 

Thanks for helping me. Have a great day!

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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