DATA Step, Macro, Functions and more

Timestamp Manipulation

Accepted Solution Solved
Reply
Contributor
Posts: 46
Accepted Solution

Timestamp Manipulation

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.

 


Accepted Solutions
Solution
‎01-05-2017 04:40 AM
Respected Advisor
Posts: 4,173

Re: Timestamp Manipulation

Posted in reply to ankit___gupta

@ankit___gupta

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;

View solution in original post


All Replies
Trusted Advisor
Posts: 1,574

Re: Timestamp Manipulation

Posted in reply to ankit___gupta

Use

 

date2 = intnx('seconds',date1,10);

Contributor
Posts: 46

Re: Timestamp Manipulation

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.

 

Contributor
Posts: 52

Re: Timestamp Manipulation

Posted in reply to ankit___gupta

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 


Cheers from India!

Manjeet
Respected Advisor
Posts: 4,173

Re: Timestamp Manipulation

Posted in reply to ankit___gupta

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;

 

Occasional Contributor
Posts: 17

Re: Timestamp Manipulation

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.

Contributor
Posts: 46

Re: Timestamp Manipulation

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

Contributor
Posts: 52

Re: Timestamp Manipulation

Posted in reply to ankit___gupta

@ankit___gupta

 

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;

Cheers from India!

Manjeet
Contributor
Posts: 46

Re: Timestamp Manipulation

Got this as the output
29 %put &date2;
1799229527.8

Converting to sasdate results in null.
Trusted Advisor
Posts: 1,574

Re: Timestamp Manipulation

[ Edited ]
Posted in reply to ankit___gupta

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.

Contributor
Posts: 46

Re: Timestamp Manipulation

@Shmuel Thank You for your solution, it worked.
Solution
‎01-05-2017 04:40 AM
Respected Advisor
Posts: 4,173

Re: Timestamp Manipulation

Posted in reply to ankit___gupta

@ankit___gupta

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;
Contributor
Posts: 46

Re: Timestamp Manipulation

@Patrick Requirement was to read the timestamp and reduce 10 sec from each. So i was unable to provide a broder picture.

anyways Thanks a LOT !!
Contributor
Posts: 52

Re: Timestamp Manipulation

Posted in reply to ankit___gupta

Same solution as @Patrick

 

Manjeet


Cheers from India!

Manjeet
☑ This topic is solved.

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

Discussion stats
  • 13 replies
  • 449 views
  • 2 likes
  • 5 in conversation