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

Hi,

 

I have a character column with $20. format. This column contains date and time. I want to split the column into two columns with one columns containing only date and the other one only the time. Could anyone please help me?

 

Sample date:

Current format: $20.

 

Date

05/02/2014 04:45:57

05/01/2014 06:00:00

05/01/2014 06:00:00

10/29/2014 20:05:00

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20
data have ;
   length dtchar $20 ;
   input dtchar $20.;
cards;
05/02/2014 04:45:57
05/01/2014 06:00:00
05/01/2014 06:00:00
10/29/2014 20:05:00
;

data want;
set have;
_temp=input(dtchar,anydtdtm21.);
date=datepart(_temp);
time=timepart(_temp);
format date date9. time time10.;
drop _:;
run;

View solution in original post

4 REPLIES 4
kiranv_
Rhodochrosite | Level 12

one way to do

time = input(scan(date,2, ' '),time8.);
format time time8.;
Tom
Super User Tom
Super User

You can easily use SUBSTR() to pull apart the data if it as clean as your examples.

Let's make some sample data out of your listing.

data have ;
   length dtchar $20 ;
   input dtchar $20.;
cards;
05/02/2014 04:45:57
05/01/2014 06:00:00
05/01/2014 06:00:00
10/29/2014 20:05:00
;

Now to make new character variables use SUBSTR().  If you want you can convert them into actual date and time variables and even combine back together into a datetime variable.

data want ;
  set have ;
  length datechar $10 timechar $8 ;
  datechar = substr(dtchar,1,10);
  timechar = substr(dtchar,12);
  date = input(datechar,mmddyy10.);
  time = input(timechar,time8.);
  dt = dhms(date,0,0,time);
  format date yymmdd10. time time8. dt datetime20. ;
run;

Results

Obs          dtchar            datechar     timechar          date        time                      dt

 1     05/02/2014 04:45:57    05/02/2014    04:45:57    2014-05-02     4:45:57      02MAY2014:04:45:57
 2     05/01/2014 06:00:00    05/01/2014    06:00:00    2014-05-01     6:00:00      01MAY2014:06:00:00
 3     05/01/2014 06:00:00    05/01/2014    06:00:00    2014-05-01     6:00:00      01MAY2014:06:00:00
 4     10/29/2014 20:05:00    10/29/2014    20:05:00    2014-10-29    20:05:00      29OCT2014:20:05:00

 

Note that it is not the FORMAT that matters for variable definitions.  What matters is what LENGTH it is defined to hold. The format attached doesn't matter unless you set it shorter than the length, in which case you might be confused about the variables contents when it prints truncated values.

 

novinosrin
Tourmaline | Level 20
data have ;
   length dtchar $20 ;
   input dtchar $20.;
cards;
05/02/2014 04:45:57
05/01/2014 06:00:00
05/01/2014 06:00:00
10/29/2014 20:05:00
;

data want;
set have;
_temp=input(dtchar,anydtdtm21.);
date=datepart(_temp);
time=timepart(_temp);
format date date9. time time10.;
drop _:;
run;
nazmul
Quartz | Level 8
Thank you everyone for your help. All of you codes work. I selected the most convenient one as the code used date and time functions

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 10845 views
  • 1 like
  • 4 in conversation