DATA Step, Macro, Functions and more

Date and Time Issue

Accepted Solution Solved
Reply
Regular Contributor
Posts: 154
Accepted Solution

Date and Time Issue

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! 


Accepted Solutions
Solution
‎03-09-2018 06:15 PM
Super User
Posts: 13,046

Re: Date and Time Issue


@Crubal wrote:

Thanks @KurtBremser

 

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


All Replies
Super User
Posts: 9,586

Re: Date and Time Issue

The datepart() function extracts the date value from a datetime. After that, you can use the month() function.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Regular Contributor
Posts: 154

Re: Date and Time Issue

Posted in reply to KurtBremser

Thanks @KurtBremser

 

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. 

Solution
‎03-09-2018 06:15 PM
Super User
Posts: 13,046

Re: Date and Time Issue


@Crubal wrote:

Thanks @KurtBremser

 

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.

 

Regular Contributor
Posts: 154

Re: Date and Time Issue

This's great. Thank you. 

Occasional Contributor
Posts: 9

Re: Date and Time Issue

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

Regular Contributor
Posts: 154

Re: Date and Time Issue

@Bhushan

 

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

Thanks!

Occasional Contributor
Posts: 9

Re: Date and Time Issue

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

 

Super Contributor
Posts: 469

Re: Date and Time Issue

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
Regular Contributor
Posts: 154

Re: Date and Time Issue

Posted in reply to SuryaKiran

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. 

 

 

Super User
Posts: 13,046

Re: Date and Time Issue


@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.

Regular Contributor
Posts: 154

Re: Date and Time Issue

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

☑ This topic is solved.

Need further help from the community? Please ask a new question.

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