BookmarkSubscribeRSS Feed
BCNAV
Quartz | Level 8

A couple of small issues:

 

1. I have a datetime26.7 variable (flightdatetime) that has values like: 26SEP2018:00:51:48.0000000

 

I need to create a new one that truncates the seconds....so I would have: 26SEP2018:00:51 as a datetime variable (no seconds)

 

 

2. I also have another datetime22.3 variable (flightdategmt) that looks like 26SEP2018:00:00:00.000.  There is also a variable called gmttime, which is character. An example is 0051. I need to be able to add the 0051 to flightdatetimegmt so it will read, as a datetime, 26SEP2018:00:51 (no seconds).

 

Thanks!

6 REPLIES 6
PaigeMiller
Diamond | Level 26
  1. use format datetime15.
  2. Convert the character 0051 to a numeric 51, then add 51*60 seconds (which is 51 minutes) to your flightdategmt value, and then use format datetime15.

See the documentation for the datetime format https://documentation.sas.com/?cdcId=pgmmvacdc&cdcVersion=9.4&docsetId=leforinforref&docsetTarget=n0...

--
Paige Miller
BCNAV
Quartz | Level 8

I had tried datetime15. for number 1....but the underlying data is still stored with the seconds?  Is the datetime15. ok as I need to compare times later without seconds

 

thx

PaigeMiller
Diamond | Level 26

Formats do not change the underlying data. They change the appearance when a human looks at it.

 

If you really want the date and time in minutes, with seconds forced to be zero, you'd have to change the underlying data, or do the comparisons so seconds are ignored. All of this is simple math.

 

To take any datetime with seconds, and "round down" to the exact minute where seconds are forced to be zero:

 

newdatetime = 60*floor(datetime/60);

 

--
Paige Miller
PeterClemmensen
Tourmaline | Level 20

You can do like this

 

1) 

 

data test1;
   dt='26SEP2018:00:51:48.0000000'dt;
   dtnew=dt;
   format dt datetime26.7 dtnew datetime15.;
run;

2)

 

data test2;
   flightdategmt='26SEP2018:00:00:00.000'dt;
   gmttime='0051';
   new_flightdategmt=sum(flightdategmt, input(cats(substr(gmttime, 1, 2), '.', substr(gmttime, 3, 2)), time5.));
   format flightdategmt datetime22.3 new_flightdategmt datetime15.;
run;
andreas_lds
Jade | Level 19
A format does not change data, only what you see. You can use intnx to “round" the value.
ballardw
Super User

@BCNAV wrote:

A couple of small issues:

 

1. I have a datetime26.7 variable (flightdatetime) that has values like: 26SEP2018:00:51:48.0000000

 

I need to create a new one that truncates the seconds....so I would have: 26SEP2018:00:51 as a datetime variable (no seconds)

 

 


Example using INTX to round to minutes:

data example;
 x='26SEP2018:00:51:48.0000000'dt;
 y=intnx('minute',x,0,'B');
 format x y datetime26.7;
run;

to do similar with a character value adding 51 minutes:

data example;
 x='26SEP2018:00:00:0.0000000';
 y=put (intnx('minute',input(x,datetime27.7),51,'B'), datetime27.7);

run;

use the appropriate format/informat for your need.

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
  • 6 replies
  • 1159 views
  • 0 likes
  • 5 in conversation