BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
nmlynar13
Calcite | Level 5

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" 

1 ACCEPTED SOLUTION

Accepted Solutions
Amir
PROC Star

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.

View solution in original post

6 REPLIES 6
Amir
PROC Star

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.

nmlynar13
Calcite | Level 5

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.

Amir
PROC Star

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.

nmlynar13
Calcite | Level 5
Hi Amir,
Yes that did output the year when I removed the white space. The only thing is that it output just the 3 dates typed in (19MAR2020, 19MAR2021, 19MAR2022). Is there a way to have to read the dates in the column "announcement_dates" to produce the new column of those years?

Thank you,
Nick
Amir
PROC Star

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.

nmlynar13
Calcite | Level 5

Update: Once I removed the 3 typed in dates it worked for my dataset. Thank you very much for the help!

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 6 replies
  • 735 views
  • 0 likes
  • 2 in conversation