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;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 7 replies
  • 472 views
  • 7 likes
  • 3 in conversation