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
;
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;
@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.
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;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.