I have a file where my date came in as a character and I need to make it a date to use it in Forecasting.
My variable date looks like this:
Friday, April 8
Saturday, April 9
Sunday, April 10
...
What code would I use to make a character variable for DOW and a second variable for the date mmdd?
Thanks,
K-
Thanks Reeza,
I do have the year in a different column (2005). I attached the file.
I tried this code and got the NewDate variable, but only got what I believe is a julian date (19449) in the cells that have a repeated date... Wednesday, Apr 13(1) and Wednesday, Apr 13(2)
--
The DOW returned an error.
--
data Spring21.DC2;
set Spring21.chcdata_catcomb;
NewDate = input(catt(scan(Date,2,","),",2005"),anydtdte.);
/* DOW = dow(y); */
proc contents data=spring21.DrewC2;
run;
data Spring21.DC2;
set Spring21.chcdata_catcomb;
NewDate = input(catt(scan(Date,2,","),",2005"),anydtdte.);
DOW = dow(newDate);
dayofweek = scan(date, 1, ",");
format newDate yymmdd10.;
run;
proc contents data=spring21.DrewC2;
run;
@KayeMcK wrote:
Thanks Reeza,
I do have the year in a different column (2005). I attached the file.
I tried this code and got the NewDate variable, but only got what I believe is a julian date (19449) in the cells that have a repeated date... Wednesday, Apr 13(1) and Wednesday, Apr 13(2)
--
The DOW returned an error.
--
data Spring21.DC2;
set Spring21.chcdata_catcomb;
NewDate = input(catt(scan(Date,2,","),",2005"),anydtdte.);
/* DOW = dow(y); */
proc contents data=spring21.DrewC2;
run;
You often have to provide an explicit length with anydtdte.
Consider:
data junk; date= "Friday, April 8"; NewDate = input(catx(',',scan(Date,2,","),"2005"),anydtdte21.); put newdate date9.; run;
since CATX strips trailing spaces like CATT and is designed to insert a string between values I prefer that but the key bit is the length of anydtdte format because it will default to 9 characters which is not enough to read a text month, day of month and a couple of spaces or commas plus the year.
Thank you so much.
I am almost there!
I have some dates that were appended with a (1) or a (2). I just need to strip that off before adding the year. Everything else is working!
the anydtdte21. the "21" is what fixed the last error I was having.
K-
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.
Ready to level-up your skills? Choose your own adventure.