BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
monikaarora
Fluorite | Level 6

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!

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

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

6 REPLIES 6
monikaarora
Fluorite | Level 6

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.

FreelanceReinh
Jade | Level 19

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.

monikaarora
Fluorite | Level 6
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
Reeza
Super User

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;
monikaarora
Fluorite | Level 6
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

sas-innovate-2024.png

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.

 

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
  • 6967 views
  • 2 likes
  • 3 in conversation