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!
@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.
The datepart() function extracts the date value from a datetime. After that, you can use the month() function.
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.
@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.
This's great. Thank you.
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 Yes Strip is a char function however datatype will be char
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;
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.
@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.
Yeah, even if I have that information, I mean. Thanks!
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.