Hello,
I am really struggling to get the first day of year in date9. format into a variable.
Can someone please tell me what i am doing wrong here and maybe what would be best.
Thanks
my code
%let date_range_min = %sysfunc(mdy(1,1,year(date())));
Log error
ERROR: Required operator not found in expression: year(date())
ERROR: Argument 3 to function MDY referenced by the %SYSFUNC or %QSYSFUNC macro function is not a number.
ERROR: Invalid arguments detected in %SYSCALL, %SYSFUNC, or %QSYSFUNC argument list. Execution of %SYSCALL statement or %SYSFUNC
or %QSYSFUNC function reference is terminated.
Thanks
Damon
Just add date9. in the syntax to get the desired result.
%let date_range_min = %sysfunc(mdy(1,1,%sysfunc(year(%sysfunc(date())))),date9.);
Eerrm, the first day of the year is 01, why do you need to do any of that. If you want to substr from a date then do:
substr(put(date(),date9.),1,2);
and you can put a % before the substr for macro.
mdy is to create dates not extract from them.
%let date_range_min=%sysfunc(mdy(1,1,2014),date9.);
Obviously you will have better ways to do it, but that is OT. For the one you have, You will need %sysfunc for every SAS functions that you used in the macro expressions:
%let date_range_min = %sysfunc(mdy(1,1,%sysfunc(year(%sysfunc(date())))));
Haikuo
Let me clarify what i am trying to do. I need to get the first day of the the current year into a variable in the date9. format. The code from Hai_kuo works to get the date value into the variable but not in a date9. format. I am very new to SAS so if there is a more efficient way to get their I would love to learn it. There is so much that i don't know about this software still.
Needed
&date_range_min=01JAN2014
Thank you all for the quick responses
Just add date9. in the syntax to get the desired result.
%let date_range_min = %sysfunc(mdy(1,1,%sysfunc(year(%sysfunc(date())))),date9.);
%let begin_day=%sysfunc(putn(%sysfunc(intnx(year, %sysfunc(today()),0, b)),date9.));
I would use the following to extract the first day of current year: (to , putn() is abundant here):
%let date_range_min = %sysfunc(intnx(year,"&sysdate9"d,0, b), date9.);
Doing so, you only need to call a SAS function once, and you get to use an existing macro variable that has the system initiating date (be aware of that though). or something I would cal it cheating:
%let date_range_min =01JAN%sysfunc(year("&sysdate"d));
Good Luck,
Haikuo
This is beautifully simple. I love efficient code. Thank you everyone for helping me here. I did not realize format was an option of the sysfunc command. I love it.
Just because there's more than one way
%let date_range_min = %sysfunc(intnx(year, %sysfunc(date()), 0, b), date9.);
%put &date_range_min.;
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.
Early bird rate extended! Save $200 when you sign up by March 31.
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.
Ready to level-up your skills? Choose your own adventure.