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

I am having problems with merging datasets with different formats of date& time. :smileyplain:

Most of the datasets' date&time data look like 2009/4/1 17:00 (character type $14. in one column), but two datasets' date&time values look like

01JAN10:17:00:00 (DATETIME, numeric type).

Is there a way that I can convert the datetime data from 01JAN10:17:00:00 (DATETIME, numeric type) to  2009/4/1 17:00 (character type $14.)?

Thanks,

Chin

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

It would be better to convert the text string to an actual datetime value if you want to be able to use them for merging.

Let's assume that your character variable that looks like a datetime value is named DATETIMEC and you want to create a new variable named DATETIME.

data want;

  set have;

  datetime=input(datetimec,anydtdtm.);

  format datetime datetime.

run;

Note that $14 is too short to store all possible values in YYYY/MM/DD HH:MM format. That would require 16 characters.

Note also that if you store datetimes as character strings without the leading zeros for month,day and hour then it will not sort in chronological order.

But given those problems if you did want to convert an actual datetime value to that format you could do it using PUT functions.

data want;

  set have;

   length datetimec $16;

  datetimec=put(datepart(datetime),yymmdds10.)||' '||put(timepart(datetime),time5.);

run;

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

It would be better to convert the text string to an actual datetime value if you want to be able to use them for merging.

Let's assume that your character variable that looks like a datetime value is named DATETIMEC and you want to create a new variable named DATETIME.

data want;

  set have;

  datetime=input(datetimec,anydtdtm.);

  format datetime datetime.

run;

Note that $14 is too short to store all possible values in YYYY/MM/DD HH:MM format. That would require 16 characters.

Note also that if you store datetimes as character strings without the leading zeros for month,day and hour then it will not sort in chronological order.

But given those problems if you did want to convert an actual datetime value to that format you could do it using PUT functions.

data want;

  set have;

   length datetimec $16;

  datetimec=put(datepart(datetime),yymmdds10.)||' '||put(timepart(datetime),time5.);

run;

Ksharp
Super User
proc format;
 picture fmt
  other='%Y/%m/%d %0H:%0M' (datatype=datetime);
run;
data _null_;
 a='01JAN10:17:00:00'dt;
 b='01APR09:17:00:00'dt;
 want=put(a-intck('dtseconds',b,a),fmt21.);
 put want=;
run;

Ksharp

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!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 2 replies
  • 1154 views
  • 4 likes
  • 3 in conversation