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

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 706 views
  • 2 likes
  • 3 in conversation