DATA Step, Macro, Functions and more

First day of year problem

Accepted Solution Solved
Reply
Contributor
Posts: 53
Accepted Solution

First day of year problem

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


Accepted Solutions
Solution
‎06-27-2014 12:06 PM
Trusted Advisor
Posts: 1,228

Re: First day of year problem

Just add date9. in the syntax to get the desired result.

%let date_range_min = %sysfunc(mdy(1,1,%sysfunc(year(%sysfunc(date())))),date9.);

View solution in original post


All Replies
Super User
Super User
Posts: 7,977

Re: First day of year problem

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.

Trusted Advisor
Posts: 1,228

Re: First day of year problem

%let date_range_min=%sysfunc(mdy(1,1,2014),date9.);

Respected Advisor
Posts: 3,156

Re: First day of year problem

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

Contributor
Posts: 53

Re: First day of year problem

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

Solution
‎06-27-2014 12:06 PM
Trusted Advisor
Posts: 1,228

Re: First day of year problem

Just add date9. in the syntax to get the desired result.

%let date_range_min = %sysfunc(mdy(1,1,%sysfunc(year(%sysfunc(date())))),date9.);

Super Contributor
Posts: 275

Re: First day of year problem


%let begin_day=%sysfunc(putn(%sysfunc(intnx(year, %sysfunc(today()),0, b)),date9.));

Respected Advisor
Posts: 3,156

Re: First day of year problem

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

Contributor
Posts: 53

Re: First day of year problem

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.

Super User
Posts: 19,855

Re: First day of year problem

Just because there's more than one way Smiley Happy

%let date_range_min = %sysfunc(intnx(year, %sysfunc(date()), 0, b), date9.);

%put &date_range_min.;

🔒 This topic is solved and locked.

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

Discussion stats
  • 9 replies
  • 1414 views
  • 8 likes
  • 6 in conversation