I have a column full of dates in the following format "04JAN2010" and would like to have it reformatted to just so the year ("2010"). I saw on a previous post about using the length function, however I got an error because its a "numeric variable"
Hi @nmlynar13,
Does the following code give you what you want?
FYI, SAS dates are stored as numerics and can be formatted to be more human readable.
/* create input data */
data have;
format my_date date9.;
input my_date date9.;
datalines;
19mar2020
19mar2021
19mar2022
;
/* format a new column to just show the year */
data want;
set have;
format my_date2 year.;
my_date2 = my_date;
run;
kind regards,
Amir.
Hi @nmlynar13,
Does the following code give you what you want?
FYI, SAS dates are stored as numerics and can be formatted to be more human readable.
/* create input data */
data have;
format my_date date9.;
input my_date date9.;
datalines;
19mar2020
19mar2021
19mar2022
;
/* format a new column to just show the year */
data want;
set have;
format my_date2 year.;
my_date2 = my_date;
run;
kind regards,
Amir.
Hi Amir,
Thank you for the response, this code does run without errors, however it deletes all data observations and produced just two columns (original date and new date) without any data in it.
data easy;
format announcement_date date9.;
input announcement_date date9.;
datalines;
19mar2020
19mar2021
19mar2022
;
data easy2;
set easy;
format date2 year.;
date2 = announcement_date;
run;
I used the same code you stated with my dataset names and column names.
Hi @nmlynar13,
If I've understood you correctly, the code you posted needs to be changed to remove the white space before the lines of data. so that it looks like:
data easy;
format announcement_date date9.;
input announcement_date date9.;
datalines;
19mar2020
19mar2021
19mar2022
;
data easy2;
set easy;
format date2 year.;
date2 = announcement_date;
run;
Kind regards,
Amir.
Hi @nmlynar13,
If I've understood you correctly, you have an existing data set (I'll call it "have") and you want a new data set (I'll call it "want") with a new column showing the year.
Try the below code and substitute your data set names to the ones you want:
/* produce new data set "want" based on existing data set "have" */
data want;
set have;
format announcement_dates2 year.;
announcement_dates2 = announcement_dates;
run;
Kind regards,
Amir.
Update: Once I removed the 3 typed in dates it worked for my dataset. Thank you very much for the help!
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.