Barite | Level 11

## How to convert partial character dates to numeric

1. I have the following partial character dates how to convert them into numeric date and   also compare  it with other date

Chardate              wantdate       year                 comparedate             flag

2019-09                SEP2019      2019                 2020-01-20               Y

2018                                          2018                 2020-01-20               Y

2021-08                 AUG2021    2021                 2020-01-20               N

2021                                          2021                 2020-01-20               N

2. One more request.  lhow to perform  if  chardate< comparedate   or year < year( comparedate) incase of  month missing then flag=Y else N

1 ACCEPTED SOLUTION

Accepted Solutions
Diamond | Level 26

## Re: How to convert partial character dates to numeric

Perhaps you could tell us, just in words, if your 5th grade teacher gave you this problem long before you learned to program a computer, what are the methods you would use to figure out if chardate< comparedate.

SAS does not allow partial dates. It must have a year, it must have a month, and it must have a day. So the best thing to do, in my opinion, is if there is no day of the month, assign it to the first day of the month. If there is no month, then assign it to the first day of the year. If you want to do this, you could use this line

``numdate = input(cats(chardate,'-01-01'),yymmdd10.);``

to create a variable named numdate, which could then be compared to any other date according to my rules above.

--
Paige Miller
3 REPLIES 3
Super User

## Re: How to convert partial character dates to numeric

To make a date out of a partial string, you must first decide which day and month you want to set as a default. So which day in which month do you want to set when only a year is given, and which day when only year and month are given?

Diamond | Level 26

## Re: How to convert partial character dates to numeric

Perhaps you could tell us, just in words, if your 5th grade teacher gave you this problem long before you learned to program a computer, what are the methods you would use to figure out if chardate< comparedate.

SAS does not allow partial dates. It must have a year, it must have a month, and it must have a day. So the best thing to do, in my opinion, is if there is no day of the month, assign it to the first day of the month. If there is no month, then assign it to the first day of the year. If you want to do this, you could use this line

``numdate = input(cats(chardate,'-01-01'),yymmdd10.);``

to create a variable named numdate, which could then be compared to any other date according to my rules above.

--
Paige Miller
Barite | Level 11

## Re: How to convert partial character dates to numeric

My bad I missed that logic. Thanks for the explanation.

Discussion stats
• 3 replies
• 985 views
• 3 likes
• 3 in conversation