Hello everyone,
I am trying to create a macro variable conditional on a date range. The condition is if todays date (according to my SAS machine) is
within a certain time period then I would create the macro variable CMT which resloves to a * .
I have tried using the %if %then but was unsuccesfull.
Here is my code:
%macro comment;
%if &sysdate >='28FEB16' and <'30JAN17' %then %let cmt=*;
%mend;
%comment;
%put &cmt;
My current output from the log is:
479 %macro comment;
480 %if &sysdate >='28FEB16' and <'30JAN17' %then %let cmt=*;
481
482 %mend;
483 %comment;
SYMBOLGEN: Macro variable SYSDATE resolves to 28DEC16
484 %put &cmt;
WARNING: Apparent symbolic reference CMT not resolved.
&cmt
I want the &cmt to turin into *.
Any direction would be much apprecated.
Mike
data _null_;
if "&sysdate"d >= "28feb2016"d and "&sysdate"d < "30jan2017"d
then call symput('cmt','*');
run;
It's much easier in a data step.
The following is not valid SAS code, either in macro or a data step.
&sysdate >='28FEB16' and <'30JAN17'
As is, SAS will do a text comparsion when you want a date comparison.
So perhaps something like the following, making sure to declare a global macro variable for cmt and set it to blank so you don't get previous values hanging around. For comparing dates you should use %sysevalf to make the numeric comparison. There may be another way to do the date comparisons.
%macro comment;
%global cmt;
%let cmt=;
%if %sysevalf("&sysdate"d >='28FEB2016'd) and %sysevalf("&sysdate"d<'30JAN2017'd) %then %let cmt=*;
%mend;
%comment;
%put &cmt;
Hello Reeza,
Thanks for your post. Your reponse gave me the answer I was looking for. The main purpose for this task is to remove certain parts of code within a time period so that specific data tables wouldn't get overwritten.
Big thanks.
data _null_;
if "&sysdate"d >= "28feb2016"d and "&sysdate"d < "30jan2017"d
then call symput('cmt','*');
run;
It's much easier in a data step.
And if you use CALL SYMPUTX the third parameter creates a global macro variable.
Yes, it is far easier (especially when reviewing the code the next day) in a data step, but sometimes you want inline macro code that doesn't require a data step or even a user-defined macro call. This macrovar assignment can be used in open code:
%let cmt=%sysfunc(ifc(
%sysfunc(inputn(01jan2017,date9.))<%sysfunc(inputn(&sysdate,date9.)) and
%sysfunc(inputn(&sysdate,date9.))<%sysfunc(inputn(31jan2017,date9.))
,*,%str( )));
%put &=cmt;
or maybe a parameterized macro call:
%macro cmt(dat1,dat2);
%if %sysfunc(inputn(&dat1,date9.))<%sysfunc(inputn(&sysdate,date9.)) and
%sysfunc(inputn(&sysdate,date9.))<%sysfunc(inputn(&dat2,date9.))
%then *;
%else %str( )
%mend;
%let cmt=%cmt(01feb2016,31jan2017);
I know - lots and lots of %SYSFUNCs, but just for completeness.
KurtBremser,
Thanks for your post. This gave me the answer I was looking for. The code provided is simplier to perform. The main goal for this task was to comment out certain parts of code within my main program so that certain data tables would not get overwritten in case someone needed to run the process again.
Happy Holidays.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.