BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
dsbihill
Obsidian | Level 7

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

1 ACCEPTED SOLUTION

Accepted Solutions
stat_sas
Ammonite | Level 13

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

9 REPLIES 9
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

stat_sas
Ammonite | Level 13

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

Haikuo
Onyx | Level 15

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

dsbihill
Obsidian | Level 7

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

stat_sas
Ammonite | Level 13

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

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

slchen
Lapis Lazuli | Level 10


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

Haikuo
Onyx | Level 15

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

dsbihill
Obsidian | Level 7

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.

Reeza
Super User

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.;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 9 replies
  • 11002 views
  • 9 likes
  • 6 in conversation