BookmarkSubscribeRSS Feed
Srigyan
Quartz | Level 8

I am looking to skip block of code based on some condition. Like if user does not enter date in the parameter of macro the following code won't run. 

 

%macro test(final_File,date);

/*Other macro*/

/****I want to skip the following Code if user doesn't enter date in parameter*****/

insert into  &final_File._checkpoints
set Checkpoints="Min Date",
Total_Count=(select Count(&date.)  from &final_File.),
Date_Check=(select min(&date.) format date9. as min_date from &final_File.),
comment="Min date of record",
Update_Date=today();
run;

/*Other macro*/

%mend;

same as we had in VBA, 

 

 If date="" then goto xx:

Block of code which will be missed if condition matches here

xx:
run another code
2 REPLIES 2
Eric_D
SAS Employee

Depending on what is needed I would probably use  %if - %then - %else block 

 

And in the first %if evaluate your inputs for validity. 

 

Example from the link below

/* capture conditional logic in macro */
%macro SummarizeIfExists();
 %if %sysfunc(exist(work.result)) %then
  %do;
    proc means data=work.result;
    run;
  %end; 
%else
  %do;
    %PUT WARNING: Missing WORK.RESULT - report process skipped.;
  %end;
%mend;
 
/* call the macro */
%SummarizeIfExists();

 

Really broken down here:

https://blogs.sas.com/content/sasdummy/2018/07/05/if-then-else-sas-programs/

Astounding
PROC Star

There are a few ways to check for a non-null value in a macro variable.  The one I like is:

 

%if %length(&date) > 0 %then %do;

 

You could insert that in the middle of your macro (with a matching %end statement at the end of the conditional code).

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