DATA Step, Macro, Functions and more

creating conditions using date range and generating a macro variable

Accepted Solution Solved
Reply
Occasional Contributor yo1
Occasional Contributor
Posts: 19
Accepted Solution

creating conditions using date range and generating a macro variable

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

 

 


Accepted Solutions
Solution
‎12-29-2016 09:44 AM
Super User
Posts: 6,972

Re: creating conditions using date range and generating a macro variable

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Super User
Posts: 17,963

Re: creating conditions using date range and generating a macro variable

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;

 

Occasional Contributor yo1
Occasional Contributor
Posts: 19

Re: creating conditions using date range and generating a macro variable

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.

 

 

Solution
‎12-29-2016 09:44 AM
Super User
Posts: 6,972

Re: creating conditions using date range and generating a macro variable

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 17,963

Re: creating conditions using date range and generating a macro variable

And if you use CALL SYMPUTX the third parameter creates a global macro variable. 

Valued Guide
Posts: 797

Re: creating conditions using date range and generating a 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.

 

 

Occasional Contributor yo1
Occasional Contributor
Posts: 19

Re: creating conditions using date range and generating a macro variable

Thanks mkeintz. Your code works as well. Its great to have variety of code for a specific task because one may run into a certain situations where one method works better (or more efficient) than others.

Happy Holidays.
Occasional Contributor yo1
Occasional Contributor
Posts: 19

Re: creating conditions using date range and generating a macro variable

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.

 

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 279 views
  • 5 likes
  • 4 in conversation