Page 1 of 1

Code to determine date of first Friday of the year

Posted: Tue Jan 11, 2022 7:48 am
by BT454
Good morning!
Is there a way to combine the @datefloor, @datepart, @weekday, and @datestr (or some combination of functions) to return a string that is the first Friday of the current year?
I'm thinking something like:
%mydate=@datestr(@datepart(@datefloor(@date),"dd"),"mm/dd/yyyy"), except I'm not sure how to point the code to the first friday.
Thanks!
Bob

Re: Code to determine date of first Friday of the year

Posted: Tue Jan 11, 2022 11:39 am
by EViews Matt
Hello,

I believe this will work:

Code: Select all

%mydate = @datestr(@datefloor(@date, "y") + 6 - @mod(@datepart(@datefloor(@date, "y"), "w") + 1, 7), "mm/dd/yyyy")

Re: Code to determine date of first Friday of the year

Posted: Tue Jan 11, 2022 1:48 pm
by BT454
Hi Matt!
Thanks for your reply! I receive a "non-numeric argument" error when trying that code. I tried adding a @datestr() to the second half, but it didn't resolve the error. Any thoughts?

Re: Code to determine date of first Friday of the year

Posted: Tue Jan 11, 2022 1:53 pm
by BT454
Got it figured out!
%mydate=@datestr(6 - @mod(@datepart(@datefloor(@dateval(@date), "y"), "w") + 1, 7),"mm/dd/yyyy")

Thanks!

Re: Code to determine date of first Friday of the year

Posted: Tue Jan 11, 2022 2:03 pm
by BT454
i actually didn't figure it out. but i think i'm close. that returns "1/7/1"

Re: Code to determine date of first Friday of the year

Posted: Tue Jan 11, 2022 3:49 pm
by EViews Matt
My apologies, I pasted a genr expression for the calculation instead of a scalar context expression. Just replace to two @date references with @now...

Code: Select all

%mydate = @datestr(@datefloor(@now, "y") + 6 - @mod(@datepart(@datefloor(@now, "y"), "w") + 1, 7), "mm/dd/yyyy")