BookmarkSubscribeRSS Feed
sasxii
Calcite | Level 5

Hi there,

 

I have a date format in DATETIME18. and would like to add three hours to it to reflect a timezone.

 

EX. 

12/31/2018 8:13:55 AM will become 12/31/2018 11:13:55 AM in the column for all the variables that fall under that column. 

 

How would you do this? At the moment I have already put my data in and it is listed as data1 with the date column being 'DATE' .

 

 

 

13 REPLIES 13
PaigeMiller
Diamond | Level 26

Three hours is 3*60*60 = 10800 seconds. So you would add 10800 to the date/time variable.

--
Paige Miller
sasxii
Calcite | Level 5

so would it be DATETIME18. + hours converted to seconds? 

PaigeMiller
Diamond | Level 26

If the variable is named DATE then

 

date = date + 10800;
--
Paige Miller
sasxii
Calcite | Level 5
Would I do it in a proc format?

proc format;
DATE = DATE + 10800;
run;
PaigeMiller
Diamond | Level 26

@sasxii wrote:
Would I do it in a proc format?

proc format;
DATE = DATE + 10800;
run;

This can only be done in a SAS DATA step. It cannot be done in PROC FORMAT.

--
Paige Miller
ChanceTGardener
SAS Employee

Another possibility:

 

data test;
 /* Recreating your datetime variable */
  have=dhms('31DEC2018'd,8,13,55);

 /* adding 3 hours */
  want=dhms(datepart(have),hour(have)+3,minute(have),second(have));

 format have want dateampm22.;
run;
ballardw
Super User

@PaigeMiller wrote:

@sasxii wrote:
Would I do it in a proc format?

proc format;
DATE = DATE + 10800;
run;

This can only be done in a SAS DATA step. It cannot be done in PROC FORMAT.


Since Proc Format will now allow use of functions from Proc Fcmp a specific value such as adding 10800 seconds or similar could be used in a format. Whether it is a good idea may be questioned for many purposes. The example in the documentation shows creating two formats to convert temperatures from Celsius to Fahrenheit and Fahrenheit to Celsius which do the numeric conversion and show an appropriate C or F at the end.

Tom
Super User Tom
Super User

@sasxii wrote:

so would it be DATETIME18. + hours converted to seconds? 


That doesn't make much sense.

DATETIME18. is a format specification, like DOLLAR10.2 or BEST12., not an actual datetime value.

Tom
Super User Tom
Super User

NOTE: If you have a datetime value it is going to be confusing to refer to it as a DATE.  DATE values are stored in number of days and DATETIME and TIME values are stored in number of seconds.

 

You can use the INTNX() function to change date/time/datetime values by specific intervals.

intnx('hour',datetime_var,3,'same')

You can use time literal to add three hours.

datetime_var + '03:00't

Or you could just add the right number of seconds.

datetime_var + 3*60*60 ;

 

In terms of reflecting timezone I would assume that you need some type of test of what timezone the current value represents to get the right number of hours added.  So perhaps you data step will look something like this.

data want ;
  set have ;
  if timezone='PT' then datetimevar=datetimevar + '03:00't ;
run;

 

 

sasxii
Calcite | Level 5
what happens if I the value is not by a specific interval? how would this be done then?
Tom
Super User Tom
Super User

@sasxii wrote:
what happens if I the value is not by a specific interval? how would this be done then?

Replace 3 in the formula with the name of the variable that has the number of hours you want to change by.

PaigeMiller
Diamond | Level 26

@sasxii wrote:
what happens if I the value is not by a specific interval? how would this be done then?

Do you mean sometimes it might be 6 hours instead of 3 hours? The same method would be used.

--
Paige Miller
Reeza
Super User
next_time = intnx('hours', originalVariable, incrementVariable);

You can use variables in the INTNX() function which seems appropriate. 


@sasxii wrote:
what happens if I the value is not by a specific interval? how would this be done then?

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 13 replies
  • 18053 views
  • 4 likes
  • 6 in conversation