Help using Base SAS procedures

Convert character to date

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 114
Accepted Solution

Convert character to date

I have a col classified character $31
Here is example.
2018-04-03:11:26:45:573000000

I use
Data test;
Set test;
Day=substr(left(opendate),1,10);
Format day mmddyy10.;
Run;

I used that b4 the input to convert it.

Accepted Solutions
Solution
‎04-05-2018 11:48 AM
Valued Guide
Posts: 589

Re: Convert character to date

data want;
   set have;
   day=INPUT(SUBSTR(left(opendate),1,10),yymmdd10.);
   format day mmddyy10.;
   run;
Thanks,
Suryakiran

View solution in original post


All Replies
Super User
Posts: 23,661

Re: Convert character to date

INPUT() converts and you can try ANYDTDTM. for a datetime

 


@Gil_ wrote:
I have a col classified character $31
Here is example.
2018-04-03:11:26:45:573000000

I use
Data test;
Set test;
Day=substr(left(opendate),1,10);
Format day mmddyy10.;
Run;

I used that b4 the input to convert it.

 

Frequent Contributor
Posts: 114

Re: Convert character to date

It converted to number here is code
Data test;
Set test;
Day_=input(day,ANYDTDTM.);
FORMAT DAY_ ANTDTDTM.;
RUN;

The output is this
1838332800
I would need it to look like 04/04/2018
Super User
Posts: 23,661

Re: Convert character to date

 You didn't apply the format correctly and since it's a date time, it creates a date time. You can display it as a date using DTDDMMYY10 or DTDATE9 or convert it using DATEPART() to get the date portion.


@Gil_ wrote:
It converted to number here is code
Data test;
Set test;
DayTime = input(day,ANYDTDTM.);
date = datepart(dayTime);

FORMAT DAY dtdate9. date ddmmyy10.;
RUN;

Solution
‎04-05-2018 11:48 AM
Valued Guide
Posts: 589

Re: Convert character to date

data want;
   set have;
   day=INPUT(SUBSTR(left(opendate),1,10),yymmdd10.);
   format day mmddyy10.;
   run;
Thanks,
Suryakiran
Super User
Posts: 6,751

Re: Convert character to date

Posted in reply to SuryaKiran

Even simpler (the FORMAT statement is still necessary):

 

day = input(opendate, yymmdd10.);

 

That assumes your character variable OPENDATE is left-hand-justified.

Frequent Contributor
Posts: 114

Re: Convert character to date

Thank you
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 223 views
  • 0 likes
  • 4 in conversation