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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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