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 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

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