Hi All,
I am trying to combine Date (eg 01/18/1985) and Time (eg 01:36 PM) in SAS. What I want is a new variable Date_Time (eg: 01/18/1985 01:36 PM). Data for date and time is given in the above example format.
I have tried a couple of options but nothing gives me the format I want. Can anyone help me with this?
Thanks!
Hi @monikaarora,
When it comes to date, time and datetime values, we have to distinguish between
From your original post it is not clear to me, which of the above three cases applies to your "Date" and "Time" values and what type of variable you want Date_Time to be (case 2 or 3?).
The DHMS function requires a SAS date value (case 3 above) as its first argument, numbers of hours, minutes and seconds as further arguments and returns a SAS datetime value (again, case 3). Hence it cannot "use colon" or any other separators. It transforms numbers into other numbers. The examples in the documentation linked by Reeza use the DATETIME. format to display the SAS datetime values in a "human-readable" way. These formatted values, of course, can contain colons and other special characters. Generally, within case 3 we must also distinguish between the actual internal value (e.g. 9149) and its various possible appearances due to formatting (e.g. 18JAN85, 01/18/1985 etc.).
So, if your datetime value at hand is of case 3 (e.g. the result of applying the DHMS function) and you want to display it in a format where date and time part are separated by a blank, we have to look into section "Date and Time" of the list of SAS Formats by Category.
There we find one format which meets this requirement (leaving NLS formats aside): MDYAMPMw.
Example:
data test;
dt='18JAN1985:13:36'dt;
format dt mdyampm.;
proc print;
run; /* Result: 1/18/1985 1:36 PM */
Would this work for you? If not, we could create a user-defined format, tailored to your needs. Or we could consider creating a character string concatenating date and time part (case 2). It depends on how you want to use the formatted value.
DHMS() function.
See last example in documentation:
http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000179419.htm
Thanks for replying Reeza.
I have tried DHMS but the problem is DHMS use colon : as a seperator b/w Date & Time but I want a space( ) b/w Date and Time.
Hi @monikaarora,
When it comes to date, time and datetime values, we have to distinguish between
From your original post it is not clear to me, which of the above three cases applies to your "Date" and "Time" values and what type of variable you want Date_Time to be (case 2 or 3?).
The DHMS function requires a SAS date value (case 3 above) as its first argument, numbers of hours, minutes and seconds as further arguments and returns a SAS datetime value (again, case 3). Hence it cannot "use colon" or any other separators. It transforms numbers into other numbers. The examples in the documentation linked by Reeza use the DATETIME. format to display the SAS datetime values in a "human-readable" way. These formatted values, of course, can contain colons and other special characters. Generally, within case 3 we must also distinguish between the actual internal value (e.g. 9149) and its various possible appearances due to formatting (e.g. 18JAN85, 01/18/1985 etc.).
So, if your datetime value at hand is of case 3 (e.g. the result of applying the DHMS function) and you want to display it in a format where date and time part are separated by a blank, we have to look into section "Date and Time" of the list of SAS Formats by Category.
There we find one format which meets this requirement (leaving NLS formats aside): MDYAMPMw.
Example:
data test;
dt='18JAN1985:13:36'dt;
format dt mdyampm.;
proc print;
run; /* Result: 1/18/1985 1:36 PM */
Would this work for you? If not, we could create a user-defined format, tailored to your needs. Or we could consider creating a character string concatenating date and time part (case 2). It depends on how you want to use the formatted value.
Yeah, I'm not sure why SAS doesn't have a format for that already.
Anyways, they have a note that shows a sample of how to customize it.
http://support.sas.com/kb/24/621.html
Working off that, here's two custom formats that generate a space. Note that this affects the display of the variable, but it's still a datetime variable, which is a good way to implement this.
proc format;
picture mydate other='%0m/%0d/%0Y %0H:%0M:%0S' (datatype=datetime);
picture reeza_date other='%d%b%Y %0H:%0M:%0S' (datatype=datetime);
run;
data a;
x='14feb2005 12:34:56'dt;
y=x;
format x mydate. y reeza_date.;;
run;
proc print;
run;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.