Hi All,
I hope everyone is doing good.
I need a datetime value for the 1st day of a year, which should look like this.
2022-01-01T00:00:00.000Z
By considering the above datetime only the year needs to be changed whenever the new year comes. The date and month should remain same i.e 01-01.
Example:
2023-01-01T00:00:00.000Z
The datetime value must be in a macro variable.
Any help would be greatly appreciated.
Thanks.
Depending on the purpose of the macro variable it is often better not to store a formatted date, time or datetime value, but the unformatted original. The formatted value is most likely completely useless, when used to filter data. And with a normal datetime you can use intnx to shift the value one year forward.
By considering the above datetime only the year needs to be changed whenever the new year comes
So whenever new data arrives, which is yearly, the year should be increased. But how are you storing the date between sessions? How does the program know that it is the second time new data arrived, and the constant "2022-01-01T00:00:00.000Z" must increased by two?
Maybe this helps, please note that i don't know the name of the datetime format displaying the datetime in the expected form.
data _null_;
date = intnx('year', today(), 1, 'b');
dt = dhms(date, 0, 0, 0);
call symputx('next_year', put(dt, datetime.));
run;
Or, if you want to work with strings:
%macro demo;
%local start next_year;
%let start = 2022-01-01T00:00:00.000Z;
%let year = %substr(&start, 1, 4);
%let year = %eval(&year + 1);
%let next_year = &year.%substr(&start, 5);
%put &=next_year;
%mend;
%demo;
@Pandu2 wrote:
Based upon the current date it should know when to change the year but not the date and month i.e 01-01.
So, based on the current year, you need year+1? This is exactly what the data step i posted does. The only thing you need to do is finding an appropriate format.
%let macrovar = %sysfunc(intnx(dtyear,%sysfunc(datetime()),0,b));
will give you the zero second of the current year as a raw value, which is usually better in macro variables. Formatted macro variables are only needed for display purposes.
Edit: replaced the TODAY function with DATETIME
See correction. Has to be DATETIME in the inner %SYSFUNC.
To get the correct format, just add it as a second parameter to %SYSFUNC.
Thought that today() returns a date, not a datetime, but maybe i am not up-to-date.
Correct. Somewhere between setting up the post in my brain and typing it into the forum DATETIME changed to TODAY 😉
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.