SAS Programming

DATA Step, Macro, Functions and more
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).

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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