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

Hi all, 

 

How can I separate date (date9.) and time (time5.) from below data:

 

Obs subj date
1   CA2099DX-1-304     2019-08-09
2   CA2099DX-1-304     2019-06-13T12:00
3   CA2099DX-1-304     2019-07-13T09:00
4   CA2099DX-1-304     2019-05-06
5   CA2099DX-108-175 2019-03-08
6   CA2099DX-108-175 2019-06-28
7   CA2099DX-108-175 2019-03-07
8   CA2099DX-108-175 2019-03-07
9   CA2099DX-108-175 2019-03-07
10 CA2099DX-108-175 2019-02-04
11 CA2099DX-108-175 2019-01-10T00:00
12 CA2099DX-108-175 2019-01-28
13 CA2099DX-108-175 2019-03-08
14 CA2099DX-108-175 2019-05-02
15 CA2099DX-108-175 2019-02-04

 

 

Thanks,

Adi

1 ACCEPTED SOLUTION

Accepted Solutions
data_null__
Jade | Level 19

Read the field with in-format B8601DT. no need to fiddle with anything.

 

datetime :b8601dt.;

View solution in original post

10 REPLIES 10
chinna0369
Pyrite | Level 9

and I want to create datetime20. by using those two new variables and if time is missing then I want to show as following format

"07MAR2019:00:00:00"

 

Thanks,

Adithya

Jagadishkatam
Amethyst | Level 16

Please try the below code

 

data have;
input Obs subj :$20. datetime:$100.;
if index(datetime,'T') then newdate=input(scan(datetime,1,'T'),yymmdd10.);
else newdate=input(datetime,yymmdd10.);
if index(datetime,'T') then time=input(scan(datetime,2,'T'),time5.);
if index(datetime,'T') then newdatetime=input(datetime,is8601dt.);
else newdatetime=input(cats(datetime,'T00:00'),is8601dt.);
format newdate date9. time time5. newdatetime datetime20.;
cards;
1   CA2099DX-1-304     2019-08-09
2   CA2099DX-1-304     2019-06-13T12:00
3   CA2099DX-1-304     2019-07-13T09:00
4   CA2099DX-1-304     2019-05-06
5   CA2099DX-108-175 2019-03-08
6   CA2099DX-108-175 2019-06-28
7   CA2099DX-108-175 2019-03-07
8   CA2099DX-108-175 2019-03-07
9   CA2099DX-108-175 2019-03-07
10 CA2099DX-108-175 2019-02-04
11 CA2099DX-108-175 2019-01-10T00:00
12 CA2099DX-108-175 2019-01-28
13 CA2099DX-108-175 2019-03-08
14 CA2099DX-108-175 2019-05-02
15 CA2099DX-108-175 2019-02-04
;

 

Thanks,
Jag
data_null__
Jade | Level 19

Read the field with in-format B8601DT. no need to fiddle with anything.

 

datetime :b8601dt.;

ballardw
Super User

@chinna0369 wrote:

Hi all, 

 

How can I separate date (date9.) and time (time5.) from below data:

 

Obs subj date
1   CA2099DX-1-304     2019-08-09
2   CA2099DX-1-304     2019-06-13T12:00
3   CA2099DX-1-304     2019-07-13T09:00
4   CA2099DX-1-304     2019-05-06
5   CA2099DX-108-175 2019-03-08
6   CA2099DX-108-175 2019-06-28
7   CA2099DX-108-175 2019-03-07
8   CA2099DX-108-175 2019-03-07
9   CA2099DX-108-175 2019-03-07
10 CA2099DX-108-175 2019-02-04
11 CA2099DX-108-175 2019-01-10T00:00
12 CA2099DX-108-175 2019-01-28
13 CA2099DX-108-175 2019-03-08
14 CA2099DX-108-175 2019-05-02
15 CA2099DX-108-175 2019-02-04

 

 

Thanks,

Adi


Is that actually the content of a SAS dataset? If so, what is the current format for that "date" variable?

If that is actually a character variable the first thing will be to read the date into a SAS date/ datetime value.

chinna0369
Pyrite | Level 9

Yes, this is SAS content. and my date variable is charecter $16. format.

I don't understand second question? can you explain it to me clearly?

 

Thanks,

Adithya

novinosrin
Tourmaline | Level 20
data have;
input Obs subj :$20. datetime :ymddttm24.;
date=datepart(datetime);
time=timepart(datetime);
format datetime datetime20. date date9. time time5.;
cards;
1   CA2099DX-1-304     2019-08-09
2   CA2099DX-1-304     2019-06-13T12:00
3   CA2099DX-1-304     2019-07-13T09:00
4   CA2099DX-1-304     2019-05-06
5   CA2099DX-108-175 2019-03-08
6   CA2099DX-108-175 2019-06-28
7   CA2099DX-108-175 2019-03-07
8   CA2099DX-108-175 2019-03-07
9   CA2099DX-108-175 2019-03-07
10 CA2099DX-108-175 2019-02-04
11 CA2099DX-108-175 2019-01-10T00:00
12 CA2099DX-108-175 2019-01-28
13 CA2099DX-108-175 2019-03-08
14 CA2099DX-108-175 2019-05-02
15 CA2099DX-108-175 2019-02-04
;
novinosrin
Tourmaline | Level 20

And if your data is in a SAS dataset, then

 


data have;
input Obs subj :$20. datetime :$24.;
cards;
1   CA2099DX-1-304     2019-08-09
2   CA2099DX-1-304     2019-06-13T12:00
3   CA2099DX-1-304     2019-07-13T09:00
4   CA2099DX-1-304     2019-05-06
5   CA2099DX-108-175 2019-03-08
6   CA2099DX-108-175 2019-06-28
7   CA2099DX-108-175 2019-03-07
8   CA2099DX-108-175 2019-03-07
9   CA2099DX-108-175 2019-03-07
10 CA2099DX-108-175 2019-02-04
11 CA2099DX-108-175 2019-01-10T00:00
12 CA2099DX-108-175 2019-01-28
13 CA2099DX-108-175 2019-03-08
14 CA2099DX-108-175 2019-05-02
15 CA2099DX-108-175 2019-02-04
;

data want;
set have;
datetime_num =input(datetime,ymddttm24.);
date=datepart(datetime_num);
time=timepart(datetime_num);
format datetime_num datetime20. date date9. time time5.;
run;
Reeza
Super User

INPUT() can be used to convert dates and times. 

You can try ANYDTDTM. as your informat and it should do a decent job at guessing the data. I think that will get you what you want and you just need to apply the formats that after the fact. 

 

Here's a great, but longer and in depth, reference for dates and times in SAS - it covers in detail how to do this and answers your question in detail.
https://communities.sas.com/t5/SAS-Communities-Library/Working-with-Dates-and-Times-in-SAS-Tutorial/...

 

 


@chinna0369 wrote:

Hi all, 

 

How can I separate date (date9.) and time (time5.) from below data:

 

Obs subj date
1   CA2099DX-1-304     2019-08-09
2   CA2099DX-1-304     2019-06-13T12:00
3   CA2099DX-1-304     2019-07-13T09:00
4   CA2099DX-1-304     2019-05-06
5   CA2099DX-108-175 2019-03-08
6   CA2099DX-108-175 2019-06-28
7   CA2099DX-108-175 2019-03-07
8   CA2099DX-108-175 2019-03-07
9   CA2099DX-108-175 2019-03-07
10 CA2099DX-108-175 2019-02-04
11 CA2099DX-108-175 2019-01-10T00:00
12 CA2099DX-108-175 2019-01-28
13 CA2099DX-108-175 2019-03-08
14 CA2099DX-108-175 2019-05-02
15 CA2099DX-108-175 2019-02-04

 

 

Thanks,

Adi


 

Reeza
Super User
Also, why do you want to do this? You can often summarize your data using just a datetime variable and using the appropriate formats in your procedures.
alexpat
Fluorite | Level 6

data jugular;
input Obs subj :$20. datetime :$24.;
cards;
1 CA2099DX-1-304 2019-08-09
2 CA2099DX-1-304 2019-06-13T12:00
3 CA2099DX-1-304 2019-07-13T09:00
4 CA2099DX-1-304 2019-05-06
5 CA2099DX-108-175 2019-03-08
6 CA2099DX-108-175 2019-06-28
7 CA2099DX-108-175 2019-03-07
8 CA2099DX-108-175 2019-03-07
9 CA2099DX-108-175 2019-03-07
10 CA2099DX-108-175 2019-02-04
11 CA2099DX-108-175 2019-01-10T00:00
12 CA2099DX-108-175 2019-01-28
13 CA2099DX-108-175 2019-03-08
14 CA2099DX-108-175 2019-05-02
15 CA2099DX-108-175 2019-02-04
;

data foramen;
set jugular;
datetime_num =input(datetime,ymddttm24.);
date=datepart(datetime_num);
time=timepart(datetime_num);
format datetime_num datetime20. date date9. time time5.;
run;

 

This solution is lot easier ...

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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
  • 10 replies
  • 1328 views
  • 8 likes
  • 7 in conversation