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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 11 replies
  • 1423 views
  • 1 like
  • 5 in conversation