I have a date in the following format
date1 = "05JAN2017:09:58:57.822"dt
I need to Subs tract 10 seconds from it. How to go about it ?
Please Help.
Thanks in Advance.
I'm a bit under the impression that you're probably asking for something that wouldn't be necessary to do and if you would give us the bigger picture and why you want to create such a second macro variable then we could eventually suggest other more direct ways to achieve the same end result.
Based on the code you've posted and what you're asking for below should work.
%let date1= "05JAN2017:09:58:57.822"dt;
data _null_;
call symputx('date2',cats('"',put(&date1-10,datetime26.3),'"dt'));
run;
%put &=date1;
%put &=date2;
Use
date2 = intnx('seconds',date1,10);
Didn't work
It is Returning Null Value.
%Let date1= "05JAN2017:09:58:57.822"dt;
%Let date2=;
data _null_;
date2 = intnx('seconds',&date1,10);
run;
%Put &date2;
date2 returns NULL.
using @Shmuel way, you will also get the result.
You just missed the call symput statement in your program, date2 macro variable is still assigned to null. Please note Date2 will be in SASDATE.
try this.
%Let date1= "05JAN2017:09:58:57.822"dt;
data _null_;
date2 = intnx('seconds',&date1,10);
Call symputx('date2',date2);
run;
%Put &date1 ;
%put &date2;
Manjeet
What @Shmuel proposes is the "clean" way of doing things.
But just for your understanding:
date1 = "05JAN2017:09:58:57.822"dt creates a SAS DateTime value which is numerical variable containing the number of seconds since 1/1/1960 - and as it's already in seconds and the variable is numerical you could also simply substract 10 seconds from it.
date2=date1-10;
I will also suggest the same thing, you can direct substract 10 seconds from your date, because your date is already saved in datetime format.
It is not working
Accoring to log it is returning NULL value.
LOG:
24 %Let date1= "05JAN2017:09:58:57.822"dt;
25 %Let date2=;
26 data _null_;
27 date2 = &date1 -10;
28 run;
NOTE: DATA statement used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
29 %Put &date2;
30
You again missed the call symput. try this:
%Let date1= "05JAN2017:09:58:57.822"dt;
%Let date2=;
data _null_;
date2 = &date1 -10;
call symput ('date2',date2);
run;
%put &date2;
To subrtact 10 seceonds from a timestamp, i.e. a datetime sas variable, you can do
either by date2 = date1 - 10;
or by date2 = intnx('seconds',date1, -10,'same');
After calculating date2, if you want it as a macro variable you can do:
either by call symput('date2', date2); or by call symput('date2', put(date2,best18.1));
or by call syput('date2', '"' || put(date2, datetime18.1) ||'"dt');
In order to check &date2, you can use %put &date2; but you shall get differen view each time.
You can check by:
data _NULL_;
date2 = &date2;
put date2= datetime18.1;
run;
and check your log - you shall get same output, doesn't matter how you created that macro variable.
I'm a bit under the impression that you're probably asking for something that wouldn't be necessary to do and if you would give us the bigger picture and why you want to create such a second macro variable then we could eventually suggest other more direct ways to achieve the same end result.
Based on the code you've posted and what you're asking for below should work.
%let date1= "05JAN2017:09:58:57.822"dt;
data _null_;
call symputx('date2',cats('"',put(&date1-10,datetime26.3),'"dt'));
run;
%put &=date1;
%put &=date2;
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.