BookmarkSubscribeRSS Feed
Pandu2
Obsidian | Level 7

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.

13 REPLIES 13
andreas_lds
Jade | Level 19

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
Obsidian | Level 7
Based upon the current date it should know when to change the year but not the date and month i.e 01-01.
andreas_lds
Jade | Level 19

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

Pandu2
Obsidian | Level 7
Yes but, will your code change the year when a new year comes.
Pandu2
Obsidian | Level 7
May I know the usage of looping here. Is it possible to achieve the required solution using current date or system date. By using current datetime the year must change whenever new year arrives and the rest should be same i.e 01-01T00:00:00.000Z
Kurt_Bremser
Super User
%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

Pandu2
Obsidian | Level 7
It just gave me 0 value. The reason I want it to be in macro variable is I use that macro variable in a url.
Kurt_Bremser
Super User

See correction. Has to be DATETIME in the inner %SYSFUNC.

To get the correct format, just add it as a second parameter to %SYSFUNC.

andreas_lds
Jade | Level 19

Thought that today() returns a date, not a datetime, but maybe i am not up-to-date.

Pandu2
Obsidian | Level 7
Thanks for the code but does your code work whenever the new year arrives?.
Ksharp
Super User
%let datetime= %sysfunc(date(),year4.)-01-01T00:00:00.000Z ;

%put &=datetime. ;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 13 replies
  • 1836 views
  • 1 like
  • 4 in conversation