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!
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;
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;
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 ?
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;
Thank you art297, it worked.
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
Thank you too.
p.s. Linlin .. Thanks for correcting my typo!
Hi Art,
you are more than welcome. Thank you for your contribution to the forum. I have learned a lot from you.
Linlin
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.
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.