Help with Macro Date Logic

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 6
Accepted Solution

Help with Macro Date Logic

Hello,

I'm trying to create a macro variable that subtracts a specific number of seconds from a datetime field in a history table based on whether yesterday falls within Daylight Savings Time or not.  I've created the following code for this:

%macro savings;

%global time;

%let date=10MAR2015 00:00:00;

%let yesterday=%sysfunc(putn(%sysfunc(datepart("&date"dt)),date9.));

%let year=%sysfunc(putn(%sysfunc(year("&yesterday"d)),best4.));

%let start=%sysfunc(putn(%sysfunc(nwkdom(2,1,3,&year)),date9.));

%let end=%sysfunc(putn(%sysfunc(nwkdom(1,1,11,&year)),date9.));

%if "&start"d<="&yesterday"d<"&end"d %then %do;

    %let time=%sysfunc(putn(18000,best8.)); %end;

%else %do;

    %let time=%sysfunc(putn(21600,best8.)); %end;

%put &time;

%mend savings;

%savings;

When I turn on the options mlogic, for some reason the "&start"d<="&yesterday"d<"&end"d part resolves to FALSE and it continues to give me my time macro variable equal to 21600; however with the symbolgen options on, the Start Variable resolves to 08MAR2015 and the end variable resolves to 01NOV2015, while my yesterday variable is resolving to 10MAR2015.  So, I know that the logic should be reading as TRUE.  I'm assuming it has something to do with the formats and SAS not reading my dates correctly in my logic statement so it defaults to FALSE and gives me the else do time value.  Does anybody know what I'm doing wrong?  Any help would be much appreciated!  Thanks in advance!

Derek


Accepted Solutions
Solution
‎03-12-2015 04:40 PM
Occasional Contributor
Posts: 6

Re: Help with Macro Date Logic

I removed my date formats I was trying to put the start, yesterday, and end values in, and the logic resolved to TRUE.  Below was my final code that gave me the correct result.  Thanks for the help.

%macro savings;

%global time;

%let yesterday=%sysfunc(datepart("&date"dt));

%let year=%sysfunc(year(&yesterday));

%let start=%sysfunc(nwkdom(2,1,3,&year));

%let end=%sysfunc(nwkdom(1,1,11,&year));

%if &start<=&yesterday<&end %then %do;

    %let time=%sysfunc(putn(18000,best8.)); %end;

%else %do;

    %let time=%sysfunc(putn(21600,best8.)); %end;

%put &time;

%mend savings;

%savings;

View solution in original post


All Replies
Super User
Posts: 5,382

Re: Help with Macro Date Logic

Macro logic is by default character based.  Control the values in the comparison to verify.

Data never sleeps
Solution
‎03-12-2015 04:40 PM
Occasional Contributor
Posts: 6

Re: Help with Macro Date Logic

I removed my date formats I was trying to put the start, yesterday, and end values in, and the logic resolved to TRUE.  Below was my final code that gave me the correct result.  Thanks for the help.

%macro savings;

%global time;

%let yesterday=%sysfunc(datepart("&date"dt));

%let year=%sysfunc(year(&yesterday));

%let start=%sysfunc(nwkdom(2,1,3,&year));

%let end=%sysfunc(nwkdom(1,1,11,&year));

%if &start<=&yesterday<&end %then %do;

    %let time=%sysfunc(putn(18000,best8.)); %end;

%else %do;

    %let time=%sysfunc(putn(21600,best8.)); %end;

%put &time;

%mend savings;

%savings;

Super User
Super User
Posts: 6,842

Re: Help with Macro Date Logic

What is with all of the %SYSFUNC(PUTN()) calls?  %SYSFUNC() already allows you to specify the format you want applied to the result of the function being called.

Also %sysfunc(putn(18000,best8.)) is just 18000 why include all of those extra characters?

%macro test(date);

%local yesterday year start end time;

%let yesterday=%sysfunc(datepart("&date"dt),date9);

%let year=%sysfunc(year("&yesterday"d),4);

%let start=%sysfunc(nwkdom(2,1,3,&year),date9);

%let end=%sysfunc(nwkdom(1,1,11,&year),date9);

%if "&start"d<="&yesterday"d<"&end"d %then %let time=18000 ;

%else %let time=21600 ;

%put _local_;

%mend ;

%test(10MAR2015 00:00:00);


TEST DATE 10MAR2015 00:00:00

TEST END 01NOV2015

TEST START 08MAR2015

TEST TIME 21600

TEST YEAR 2015

TEST YESTERDAY 10MAR2015

Occasional Contributor
Posts: 6

Re: Help with Macro Date Logic

Tom,

I need the time variable to read as numeric because I'm going to be subtracting that amount of seconds from a datetime variable using the intnx() function.  I suppose the putn() function was not needed because you can specify a format in the %sysfunc() function.  I was under the assumption of just setting the time variable equal to 18000, it would read as character and possibly error when subtracting that value from the datetime variable.  Thanks for your input!

Respected Advisor
Posts: 3,786

Re: Help with Macro Date Logic

I don't see why you're doing this with MACRO.  Seems like MACRO only complicates and otherwise simple DATA STEP application.

Super User
Super User
Posts: 6,842

Re: Help with Macro Date Logic

All macro variables are character.  But when you use them to generate SAS code the SAS compiler will interpret the generated code the same as it would if you had typed it.

So if TIME=18000 then this reference will be seen as a number:

offset= &time ;

While this reference would be seen as a string:

offset = "&time" ;

🔒 This topic is solved and locked.

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

Discussion stats
  • 6 replies
  • 309 views
  • 6 likes
  • 4 in conversation