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

Hi I have a dataset A with this format

11JAN21:13:15:00.

12JUL02:12:30:02

I want to convert this to separate column of date and time(am and pm).

Date                          Time

01/11/2021            1:15:00pm

07/12/2002         12:30:02am

1 ACCEPTED SOLUTION

Accepted Solutions
SASJedi
SAS Super FREQ

Try the DATEPART and TIMEPART functions:

data want;
input DateTimeValue:datetime.;
Date=datepart(DateTimeValue);
Time=timepart(DateTimeValue);
format DateTimeValue datetime. Date mmddyy10. time timeampm.;
datalines;
11JAN21:13:15:00
12JUL02:12:30:02
;

Result:

Obs DateTimeValue Date Time
1 11JAN21:13:15:00 01/11/2021 1:15:00 PM
2 12JUL02:12:30:02 07/12/2002 12:30:02 PM
Check out my Jedi SAS Tricks for SAS Users

View solution in original post

3 REPLIES 3
LinusH
Tourmaline | Level 20

Use the datepart and timepart functions respectively.

Data never sleeps
SASJedi
SAS Super FREQ

Try the DATEPART and TIMEPART functions:

data want;
input DateTimeValue:datetime.;
Date=datepart(DateTimeValue);
Time=timepart(DateTimeValue);
format DateTimeValue datetime. Date mmddyy10. time timeampm.;
datalines;
11JAN21:13:15:00
12JUL02:12:30:02
;

Result:

Obs DateTimeValue Date Time
1 11JAN21:13:15:00 01/11/2021 1:15:00 PM
2 12JUL02:12:30:02 07/12/2002 12:30:02 PM
Check out my Jedi SAS Tricks for SAS Users
Tom
Super User Tom
Super User

Assuming you have a NUMERIC variable that has a count of seconds that has the DATETIME16. format attached to it so that the numbers are displayed as the strings you showed.

 

Then just use the DATEPART() and TIMEPART() functions to generate new variables that contain days and seconds.  You can then attach appropriate formats to those new variables to have the values displayed in the style you showed.

 

Let's make a sample dataset with a datetime variable.

data have;
  datetime=datetime();
  format datetime datetime19.;
run;

Now we can make a new dataset that adds the new date and time variables.

data want;
  set have;
  date=datepart(datetime);
  time=timepart(datetime);
  format date mmddyy10. time timeampm. ;
run;

Result

Obs               datetime          date       time

 1      05JUN2023:09:36:06    06/05/2023    9:36:06 AM

sas-innovate-2024.png

📢

ANNOUNCEMENT

The early bird rate has been extended! Register by March 18 for just $695 - $100 off the standard rate.

 

Check out the agenda and get ready for a jam-packed event featuring workshops, super demos, breakout sessions, roundtables, inspiring keynotes and incredible networking events. 

 

Register now!

From SAS Users blog
Want more? Visit our blog for more articles like these.
5 Steps to Your First Analytics Project Using SAS

For SAS newbies, this video is a great way to get started. James Harroun walks through the process using SAS Studio for SAS OnDemand for Academics, but the same steps apply to any analytics project.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 267 views
  • 2 likes
  • 4 in conversation