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.
@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);
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.
@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);
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!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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: