BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
yo1
Obsidian | Level 7 yo1
Obsidian | Level 7

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

 

 

1 ACCEPTED SOLUTION
7 REPLIES 7
Reeza
Super User

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;

 

yo1
Obsidian | Level 7 yo1
Obsidian | Level 7

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.

 

 

Reeza
Super User

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

mkeintz
PROC Star

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.

 

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
yo1
Obsidian | Level 7 yo1
Obsidian | Level 7
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.
yo1
Obsidian | Level 7 yo1
Obsidian | Level 7

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.

 

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

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
  • 7 replies
  • 978 views
  • 5 likes
  • 4 in conversation