Help using Base SAS procedures

How to convert numeric date to sasdate ?

Accepted Solution Solved
Reply
Contributor
Posts: 40
Accepted Solution

How to convert numeric date to sasdate ?

I have a variable in a dataset with value like "ccyy-mm-dd" as character. How can I convert to date ?

I have used input(date,yymmdd10.);, but using this I am still getting variable type as character with a value like a numeric number, not date value. Please advise.

thanks in advance!


Accepted Solutions
Solution
‎01-05-2012 03:10 PM
PROC Star
Posts: 7,356

Re: How to convert numeric date to sasdate ?

Same as my example.  You can't change a variable from character to numeric, but you can rename it and create a new variable with the old variable name, and drop the old variable.  So, using the same example, assuming your current data are in a SAS dataset called have:

data want (drop=_Smiley Happy;

  format date date9.;

  set have (rename=(mydate=_mydate));

  date=input(_mydate,yymmdd10.);

run;

View solution in original post


All Replies
PROC Star
Posts: 7,356

How to convert numeric date to sasdate ?

Not sure why you are running into a problem.  The following works for me:

data have;

  input date $10.;

  cards;

2011-01-01

2011-01-02

;

data want (drop=_Smiley Happy;

  format date date9.;

  set have (rename=(date=_date));

  date=input(_date,yymmdd10.);

run;

Contributor
Posts: 40

How to convert numeric date to sasdate ?

Thanks for quick response. I am getting error "Variable mydate has been defined as both character and numeric"

I have variable "mydate" in a dataset, I am not passing inline data using cards statement.

But when I did proc datasets on my dataset, I see variable as character type with length as 30. Any insight what's wrong I am doing in here ?

Solution
‎01-05-2012 03:10 PM
PROC Star
Posts: 7,356

Re: How to convert numeric date to sasdate ?

Same as my example.  You can't change a variable from character to numeric, but you can rename it and create a new variable with the old variable name, and drop the old variable.  So, using the same example, assuming your current data are in a SAS dataset called have:

data want (drop=_Smiley Happy;

  format date date9.;

  set have (rename=(mydate=_mydate));

  date=input(_mydate,yymmdd10.);

run;

Contributor
Posts: 40

How to convert numeric date to sasdate ?

Thank you art297, it worked.

Super Contributor
Posts: 1,636

Re: How to convert numeric date to sasdate ?

data have;

  input mydate $10.;

  cards;

2011-01-01

2011-01-02

;

data want (drop=_Smiley Happy;

  format mydate date9.;

  set have (rename=(mydate=_mydate));

  mydate=input(_mydate,yymmdd10.);

run;

proc print;run;

proc contents; run;

Linlin

Contributor
Posts: 40

How to convert numeric date to sasdate ?

Thank you too.

PROC Star
Posts: 7,356

Re: How to convert numeric date to sasdate ?

p.s.  Linlin .. Thanks for correcting my typo!

Super Contributor
Posts: 1,636

Re: How to convert numeric date to sasdate ?

Hi Art,

you are more than welcome. Thank you for your contribution to the forum. I have learned a lot from you.

Linlin

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 10755 views
  • 2 likes
  • 3 in conversation