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

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.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

@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

13 REPLIES 13
Shmuel
Garnet | Level 18

Use

 

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

ankit___gupta
Quartz | Level 8

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.

 

mnjtrana
Pyrite | Level 9

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
Patrick
Opal | Level 21

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;

 

shubhayog
Obsidian | Level 7

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.

ankit___gupta
Quartz | Level 8

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

mnjtrana
Pyrite | Level 9

@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
ankit___gupta
Quartz | Level 8
Got this as the output
29 %put &date2;
1799229527.8

Converting to sasdate results in null.
Shmuel
Garnet | Level 18

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.

ankit___gupta
Quartz | Level 8
@Shmuel Thank You for your solution, it worked.
Patrick
Opal | Level 21

@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;
ankit___gupta
Quartz | Level 8
@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 !!
mnjtrana
Pyrite | Level 9

Same solution as @Patrick

 

Manjeet


Cheers from India!

Manjeet

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
  • 13 replies
  • 1607 views
  • 2 likes
  • 5 in conversation