BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sasbasls
Calcite | Level 5

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!

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

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=_:);

  format date date9.;

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

  date=input(_mydate,yymmdd10.);

run;

View solution in original post

8 REPLIES 8
art297
Opal | Level 21

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=_:);

  format date date9.;

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

  date=input(_date,yymmdd10.);

run;

sasbasls
Calcite | Level 5

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 ?

art297
Opal | Level 21

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=_:);

  format date date9.;

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

  date=input(_mydate,yymmdd10.);

run;

sasbasls
Calcite | Level 5

Thank you art297, it worked.

Linlin
Lapis Lazuli | Level 10

data have;

  input mydate $10.;

  cards;

2011-01-01

2011-01-02

;

data want (drop=_:);

  format mydate date9.;

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

  mydate=input(_mydate,yymmdd10.);

run;

proc print;run;

proc contents; run;

Linlin

sasbasls
Calcite | Level 5

Thank you too.

art297
Opal | Level 21

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

Linlin
Lapis Lazuli | Level 10

Hi Art,

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

Linlin

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 8 replies
  • 17988 views
  • 2 likes
  • 3 in conversation