BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
mona4u
Lapis Lazuli | Level 10

Hi all, 

I am trying to convert a character date to  yyyy-mm-dd data format 

this is a sample of the data that I have 

data have;
input start_date $;
datalines;
15/Nov/2015
;

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

First of all: testing of code is not a crime, but a necessity.

If you run your code:

data have;
input start_date $;
datalines;
15/Nov/2015
;
run;

and look at the resulting dataset, you will find that the character date has been truncated to 8 characters. This is because you did not explicitly set a length (Maxim 47) and SAS therefore defaulted to 8.

With a very slight change, the code works:

data have;
input start_date :$11.;
datalines;
15/Nov/2015
;
run;

By simply changing the informat to a date format, and assigning the wanted display format, you will achieve what you want:

data have;
input start_date :date11.;
format start_date yymmddd10.;
datalines;
15/Nov/2015
;
run;

or by running the following conversion step on your original data:

data have;
input start_date :$11.;
datalines;
15/Nov/2015
;
run;

data want;
set have (rename=(start_date=_start_date));
start_date = input(_start_date,date11.);
format start_date yymmddd10.;
drop _start_date;
run;

View solution in original post

3 REPLIES 3
Reeza
Super User
COMPRESS() to remove the / and INPUT() with DATE9 to read it in.

want = input(compress(start_date, '/'), date9.);
ballardw
Super User

@mona4u wrote:

Hi all, 

I am trying to convert a character date to  yyyy-mm-dd data format 

this is a sample of the data that I have 

data have;
input start_date $;
datalines;
15/Nov/2015
;


Please not that SAS documentation is pretty consistent about using MON for abbreviated month names. So your question and title would be better phrased as "convert character date in dd/mon/yyyy format to a date value". Once you have a date value then format used is situational.

Kurt_Bremser
Super User

First of all: testing of code is not a crime, but a necessity.

If you run your code:

data have;
input start_date $;
datalines;
15/Nov/2015
;
run;

and look at the resulting dataset, you will find that the character date has been truncated to 8 characters. This is because you did not explicitly set a length (Maxim 47) and SAS therefore defaulted to 8.

With a very slight change, the code works:

data have;
input start_date :$11.;
datalines;
15/Nov/2015
;
run;

By simply changing the informat to a date format, and assigning the wanted display format, you will achieve what you want:

data have;
input start_date :date11.;
format start_date yymmddd10.;
datalines;
15/Nov/2015
;
run;

or by running the following conversion step on your original data:

data have;
input start_date :$11.;
datalines;
15/Nov/2015
;
run;

data want;
set have (rename=(start_date=_start_date));
start_date = input(_start_date,date11.);
format start_date yymmddd10.;
drop _start_date;
run;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 16607 views
  • 1 like
  • 4 in conversation