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

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-

1 ACCEPTED SOLUTION

Accepted Solutions
KayeMcK
Fluorite | Level 6
Thanks again !!!
It may be rough, but I split the variable again and it works!!!
THANK YOU!!!

data DC2;
set chcdata_catcomb;
Date1 = scan(Date,2,",");
Date2 = scan(Date1,1,"(");
NewDate = input(catt(Date2,",2005"),anydtdte21.);
dayofweek = scan(date, 1, ",");
format NewDate yymmdd10.;
run;
proc contents data=DC2;
run;

View solution in original post

7 REPLIES 7
Reeza
Super User
Do you have a year component? Did you try ANYDTDTE?

If not, add a year component and give that a try:

x = input(catt(orig_variable, ", 2021"), anydtdte.);

Otherwise, the SCAN() function will separate the components.

date = input(catt(scan(orig_variable, 2, ","), ", 2021"), anydtdte.);
DOW = dow(y);
KayeMcK
Fluorite | Level 6

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;

KayeMcK
Fluorite | Level 6
Sorry, code here...
data Spring21.DC2;
set Spring21.chcdata_catcomb;
NewDate = input(catt(scan(Date,2,","),",2005"),anydtdte.);
/* DOW = dow(y); */
proc contents data=spring21.DC2;
run;
Reeza
Super User
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;
ballardw
Super User

@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.

KayeMcK
Fluorite | Level 6

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-

KayeMcK
Fluorite | Level 6
Thanks again !!!
It may be rough, but I split the variable again and it works!!!
THANK YOU!!!

data DC2;
set chcdata_catcomb;
Date1 = scan(Date,2,",");
Date2 = scan(Date1,1,"(");
NewDate = input(catt(Date2,",2005"),anydtdte21.);
dayofweek = scan(date, 1, ",");
format NewDate yymmdd10.;
run;
proc contents data=DC2;
run;

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

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.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

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

View all other training opportunities.

Discussion stats
  • 7 replies
  • 249 views
  • 7 likes
  • 3 in conversation