DATA Step, Macro, Functions and more

How to convert dates

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 76
Accepted Solution

How to convert dates

I have dates in two different format. One is 19861031 (YYMMDDN8). and the other  31Oct1986 (DATE9). Can somebody tell me how to make the two dates comparable and in the same format.


Accepted Solutions
Solution
‎01-10-2018 07:53 PM
PROC Star
Posts: 1,400

Re: How to convert dates

Posted in reply to Agent1592

Assumed that the two variables are actually sas date values (Formatted numeric values), then they are already comparable in the sense that you can compare them with comparison operators in SAS.

 

You can make the two dates the same format with a simple format statement like this

 

data have;
input date1:yymmdd10. date2:date9.;
format date1 yymmdd10. date2 date9.;
datalines;
19861031 31Oct1986
;

data want;
	set have;
	format date1 date9.;
run;

View solution in original post


All Replies
Super User
Posts: 23,958

Re: How to convert dates

[ Edited ]
Posted in reply to Agent1592

Are they different variables or the same variable? What is the type and format of each variable? What are you trying to do with them that makes you think they can't be compared as is.

 


Agent1592 wrote:

I have dates in two different format. One is 19861031 (YYMMDDN8). and the other  31Oct1986 (DATE9). Can somebody tell me how to make the two dates comparable and in the same format.


 

Super User
Posts: 2,496

Re: How to convert dates

Posted in reply to Agent1592

The dates are already comparable. Since you say that they have date formats, they are SAS dates.

Just use the format statement to change their format.

Solution
‎01-10-2018 07:53 PM
PROC Star
Posts: 1,400

Re: How to convert dates

Posted in reply to Agent1592

Assumed that the two variables are actually sas date values (Formatted numeric values), then they are already comparable in the sense that you can compare them with comparison operators in SAS.

 

You can make the two dates the same format with a simple format statement like this

 

data have;
input date1:yymmdd10. date2:date9.;
format date1 yymmdd10. date2 date9.;
datalines;
19861031 31Oct1986
;

data want;
	set have;
	format date1 date9.;
run;
☑ This topic is solved.

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

Discussion stats
  • 3 replies
  • 211 views
  • 1 like
  • 4 in conversation