BookmarkSubscribeRSS Feed
Jack_Smitherson
Fluorite | Level 6

Hi everyone, 

 

I want to convert a date variable with the following formatting 01JAN2008 (the format is Date9.)  to a date variable of year only (i.e. 2008). 

 

Could you please provide some guidance on the syntax to do that? 

 

Thanks in advance! 

4 REPLIES 4
stat_sas
Ammonite | Level 13

Hi,

 

Something like this?

 

data have;
d = '01JAN2008'd;
format d date9.;
year=year(d);
run;

Reeza
Super User

You have a few options. 

 

1. Convert it to a numerical variable, which is basically the year function

 

myDate = '01Jan2008'd;

myYear = year(myDate);

2. You can use a format instead to change the appearance. PROC MEANS/FREQ/SUMMARY will respect the format, PROC SQL will not. This changes how the variable is display but not the underlying data.

 

format myDate year4.;

3. You can do a combination of 1/2, supposing you wanted it as year but in a different variable.

 

myYear = myDate;
format myYear year4.;

 

Jack_Smitherson
Fluorite | Level 6

Thank you for your response. I do want to run some proc sql later on, so simply formatting the variable is not the most optimal option.

 

In the first option you gave me "Convert it to a numerical variable, which is basically the year function" - how can I go about applying that to 12 months (i.e. 01Jan2000, 1Feb2000, 01Mar2000, etc.) with five years worth of data? (i.e. 2000-2004; I have a combination of 01Jan2000, 01Jan2001, 01Jan2002, etc.). 

 

Thanks again! 

 

 

Reeza
Super User

@Jack_Smitherson wrote:

Thank you for your response. I do want to run some proc sql later on, so simply formatting the variable is not the most optimal option.

 

In the first option you gave me "Convert it to a numerical variable, which is basically the year function" - how can I go about applying that to 12 months (i.e. 01Jan2000, 1Feb2000, 01Mar2000, etc.) with five years worth of data? (i.e. 2000-2004; I have a combination of 01Jan2000, 01Jan2001, 01Jan2002, etc.). 

 

Thanks again! 

 

 


That depends on your data structure.  

Use the YEAR() function on the variable to obtain the YEAR. 

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 7836 views
  • 0 likes
  • 3 in conversation