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' .
Three hours is 3*60*60 = 10800 seconds. So you would add 10800 to the date/time variable.
so would it be DATETIME18. + hours converted to seconds?
If the variable is named DATE then
date = date + 10800;
@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.
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;
@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.
@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.
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 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.
@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.
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?
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.