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).

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 2753 views
  • 0 likes
  • 3 in conversation