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

In a table that I have loaded, the dates are written as character format. For instance: 

201601 represents year 2020, date 16, month 1 [16 january 2020]. 

Is there any way I could transform this to a date format so I could do ordinary date calculations? 

Thanks. 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

@SasStatistics wrote:

In a table that I have loaded, the dates are written as character format. For instance: 

201601 represents year 2020, date 16, month 1 [16 january 2020]. 

Is there any way I could transform this to a date format so I could do ordinary date calculations? 


So, first, let's clear up something, you don't need any particular format to do date calculations. You do need to have the date value represented as an integer (which is a numeric variable) whose value is the number of days since 01JAN1960. 

 

Ignoring (for a minute) the valid concern expressed by @ballardw about your two-digit years, you can convert this to the a numeric date like this:

 

numeric_date = mdy(input(substr(char_date,5,2),2.),
    input(substr(char_date,3,2),2.),
    input(substr(char_date,1,2),2.)+2000);

 

--
Paige Miller

View solution in original post

2 REPLIES 2
ballardw
Super User

You will need to create a new variable

Since there is no standard date that does year day month you will need to pull that value apart to get the month, day and year values and use the MDY function to create the date value.

 

Do your values always use two digits for the day of month and month of the year? If not you are going to have to provide a lot more examples and what dates they actually represent.

 

PaigeMiller
Diamond | Level 26

@SasStatistics wrote:

In a table that I have loaded, the dates are written as character format. For instance: 

201601 represents year 2020, date 16, month 1 [16 january 2020]. 

Is there any way I could transform this to a date format so I could do ordinary date calculations? 


So, first, let's clear up something, you don't need any particular format to do date calculations. You do need to have the date value represented as an integer (which is a numeric variable) whose value is the number of days since 01JAN1960. 

 

Ignoring (for a minute) the valid concern expressed by @ballardw about your two-digit years, you can convert this to the a numeric date like this:

 

numeric_date = mdy(input(substr(char_date,5,2),2.),
    input(substr(char_date,3,2),2.),
    input(substr(char_date,1,2),2.)+2000);

 

--
Paige Miller

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 2 replies
  • 304 views
  • 2 likes
  • 3 in conversation