Code to determine date of first Friday of the year

For questions regarding programming in the EViews programming language.

Moderators: EViews Gareth, EViews Jason, EViews Moderator, EViews Matt

BT454
Posts: 135
Joined: Wed Aug 02, 2017 6:07 am

Code to determine date of first Friday of the year

Postby BT454 » Tue Jan 11, 2022 7:48 am

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

EViews Matt
EViews Developer
Posts: 557
Joined: Thu Apr 25, 2013 7:48 pm

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

Postby EViews Matt » Tue Jan 11, 2022 11:39 am

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")

BT454
Posts: 135
Joined: Wed Aug 02, 2017 6:07 am

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

Postby BT454 » Tue Jan 11, 2022 1:48 pm

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?

BT454
Posts: 135
Joined: Wed Aug 02, 2017 6:07 am

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

Postby BT454 » Tue Jan 11, 2022 1:53 pm

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

Thanks!

BT454
Posts: 135
Joined: Wed Aug 02, 2017 6:07 am

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

Postby BT454 » Tue Jan 11, 2022 2:03 pm

i actually didn't figure it out. but i think i'm close. that returns "1/7/1"

EViews Matt
EViews Developer
Posts: 557
Joined: Thu Apr 25, 2013 7:48 pm

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

Postby EViews Matt » Tue Jan 11, 2022 3:49 pm

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")


Return to “Programming”

Who is online

Users browsing this forum: No registered users and 14 guests