DATA Step, Macro, Functions and more

how to convert character date variable having format yyyy-mm into numeric date variable

Reply
Occasional Contributor
Posts: 13

how to convert character date variable having format yyyy-mm into numeric date variable

Hi,

 

I have a column Named  "Date" which is in format YYYY-MM  and they are stored in character value .

I want to convert them into numeric format so that I can create a new variable called "Newdate" to store SAS date value.

 

Thanks in Advance.

Super User
Posts: 6,928

Re: how to convert character date variable having format yyyy-mm into numeric date variable

You have to make a decision which day to use for an incomplete date.

If that should be the first of the month, do

newdate = input(date !! '-01',yymmdd10.);
format newdate yymmddd10.;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor
Posts: 13

Re: how to convert character date variable having format yyyy-mm into numeric date variable

Hi  KurtBremser,

 

Thank you very much for your  response!!!  it worked perfectly I really appreciate your help 

 

Again, thanks so much!!

 

Best,

Gowtham

Super User
Super User
Posts: 7,392

Re: how to convert character date variable having format yyyy-mm into numeric date variable

Dates are stored as number of days since a given time.  So to convert your text into a number SAS needs to be able to calculate how many days since that date there are, and without day value it cannot do this, hence it cannot convert to numeric date.  The same thing happens with times, so seconds since.  Give it a default number, then you can always apply a format - i.e. change the display of the underlying data - to YYYY-MM.

Occasional Contributor
Posts: 13

Re: how to convert character date variable having format yyyy-mm into numeric date variable

Hi RW9,

This is great!!!
Thank you so much!!!
I really appreciate your help

Again, thanks so much!!
Ask a Question
Discussion stats
  • 4 replies
  • 98 views
  • 1 like
  • 3 in conversation