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

Hi,

 

I get trouble in date-time change issue. I would like to grab month and date information from datetime. 

 

I would like to transfer table a, column 'End_Date' from '15MAR2001:00:00:00' to a new column 'End_Date_1': '15MAR' or '03-15'. 

How could I do it? 

 

Similarly, if I have a table b, column name 'Start_Date', from '2016-03-09' to a new column 'Start_Date_1': '09MAR' or '03-15'. 

I hope new column's data type is 'date'. 

 

Is there any way to do that?

 

Thanks! 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

@Crubal wrote:

Thanks @Kurt_Bremser

 

I used  

month(datepart(End_Date)) * 100 + day(datepart(End_Date)) 

to obtain the new date. And the output format looks like '101' / '315' ... 

 

Although looks not pretty, I could compare anyway. 


If you want to compare things it is likely best to leave them as date values instead of creating ugly character variables.

If you want to see the date in a specific format there are a large number of date formats supplied by SAS. For example:

data example;
   x='14MAR2018'd;
   format x mmddyyd5.;
run;

will display the date value 14Mar2018 as 03-14. The last d in the format says to place a dash (-) between the values of month, day and year. The 5 says to only display the first 5 characters of the result.

 

View solution in original post

11 REPLIES 11
Crubal
Quartz | Level 8

Thanks @Kurt_Bremser

 

I used  

month(datepart(End_Date)) * 100 + day(datepart(End_Date)) 

to obtain the new date. And the output format looks like '101' / '315' ... 

 

Although looks not pretty, I could compare anyway. 

ballardw
Super User

@Crubal wrote:

Thanks @Kurt_Bremser

 

I used  

month(datepart(End_Date)) * 100 + day(datepart(End_Date)) 

to obtain the new date. And the output format looks like '101' / '315' ... 

 

Although looks not pretty, I could compare anyway. 


If you want to compare things it is likely best to leave them as date values instead of creating ugly character variables.

If you want to see the date in a specific format there are a large number of date formats supplied by SAS. For example:

data example;
   x='14MAR2018'd;
   format x mmddyyd5.;
run;

will display the date value 14Mar2018 as 03-14. The last d in the format says to place a dash (-) between the values of month, day and year. The 5 says to only display the first 5 characters of the result.

 

Crubal
Quartz | Level 8

This's great. Thank you. 

Bhushan
Calcite | Level 5

data new;
date='19APR2018'D;
Format date date9.;
Date1=month(date);
Date2=day(date);
New=Strip(put(Date1,$5.))||"-"||Strip(put(Date2,$5.));
run;

Crubal
Quartz | Level 8

@Bhushan

 

Will 'strip' function in your code return 'string' data type?

Thanks!

Bhushan
Calcite | Level 5

@Crubal Yes Strip is a char function however datatype will be char 

 

SuryaKiran
Meteorite | Level 14

If you have datetime values then extract date values using datetime() and apply format DATE5.

Date5. format will show you only the first 5 places.

DATA HAVE;
format DATETIME_val DATE5.;
DATETIME_val=DATEPART(INPUT('15MAR2001:00:00:00',datetime24.));
run;

 

Thanks,
Suryakiran
Crubal
Quartz | Level 8

Hi @SuryaKiran

 

I try to replace it with old column name 'End_Date':

DATA HAVE;
format DATETIME_val DATE5.;
DATETIME_val=DATEPART(INPUT(End_Date,datetime24.));
run;

It seems the new column 'DATETIME_val' is empty. 

 

 

ballardw
Super User

@Crubal wrote:

Hi @SuryaKiran

 

I try to replace it with old column name 'End_Date':

DATA HAVE;
format DATETIME_val DATE5.;
DATETIME_val=DATEPART(INPUT(End_Date,datetime24.));
run;

It seems the new column 'DATETIME_val' is empty. 

 

 


NO set statement to use your existing data set. So END_DATE has no value. You should have noticed that in the "have" set.

Crubal
Quartz | Level 8

Yeah, even if I have that information, I mean. Thanks!

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
  • 11 replies
  • 2043 views
  • 1 like
  • 5 in conversation