DATA Step, Macro, Functions and more

Combining Date and Time in SAS

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 7
Accepted Solution

Combining Date and Time in SAS

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!


Accepted Solutions
Solution
‎03-03-2016 01:14 PM
Trusted Advisor
Posts: 1,117

Re: Combining Date and Time in SAS

Posted in reply to monikaarora

Hi @monikaarora,

 

When it comes to date, time and datetime values, we have to distinguish between

  1. raw data values (e.g. the text 01/18/1985 in a CSV file)
  2. strings such as '01/18/1985', typically stored in SAS character variables
  3. SAS date, time and datetime values, which are numeric and either a number of days (date) or seconds (time, datetime).

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.

View solution in original post


All Replies
Super User
Posts: 19,815

Re: Combining Date and Time in SAS

Posted in reply to monikaarora
Occasional Contributor
Posts: 7

Re: Combining Date and Time in SAS

[ Edited ]

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.

Solution
‎03-03-2016 01:14 PM
Trusted Advisor
Posts: 1,117

Re: Combining Date and Time in SAS

Posted in reply to monikaarora

Hi @monikaarora,

 

When it comes to date, time and datetime values, we have to distinguish between

  1. raw data values (e.g. the text 01/18/1985 in a CSV file)
  2. strings such as '01/18/1985', typically stored in SAS character variables
  3. SAS date, time and datetime values, which are numeric and either a number of days (date) or seconds (time, datetime).

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.

Occasional Contributor
Posts: 7

Re: Combining Date and Time in SAS

Posted in reply to FreelanceReinhard
Thanks for the solution. It worked perfectly fine!
I would also like to thank you for the detailed description. I have tried 'mdyampm' format earlier for this problem but it didn't work. Now, I know the reason. Thanks again.
Monika
Super User
Posts: 19,815

Re: Combining Date and Time in SAS

Posted in reply to monikaarora

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;
Occasional Contributor
Posts: 7

Re: Combining Date and Time in SAS

Thanks for the solution @Reeza. This also does work in my case. Also, I would like to mention I dint know about picture statement in proc format. Thanks for letting me know about the statement.
Monika
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 1817 views
  • 2 likes
  • 3 in conversation