BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
user34567
Obsidian | Level 7

Hi,

In my dataset I have a date (document_date) listed in format DATE9. I would like to add an additional column which gives you the YEAR of 'document_date'. I've tried the following things, but it is not giving me the year. What am I doing wrong?

1. Add a computed column YEAR(t1.document_date) in format YEAR4 --> gives me 17JUL1965

2. Add t1.document_date as an additional column and give it format YEAR4 --> the output is the same as the input

 

Thank you in advance.

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Both the YEAR function and the YEAR format expect an input value which represents a SAS date. SAS stores dates as counts of days, with 1960-01-01 being day zero.

Since the result of the YEAR function is not a SAS date, but the year number, applying the DATE format to the result will interpret this number as a count of days and come up with a year of 1965 for the value of 2024.

For the display of a year value like the number 2024, no format is needed. It's just a 4-digit number, unless you reached so far into the future that you went past 9999.

View solution in original post

3 REPLIES 3
Patrick
Opal | Level 21

Your 2nd options sounds like something that should work.

data have;
  document_date='25Feb2024'd;
run;

proc sql;
  select 
    document_date
    ,document_date as document_date_2 format=date9.
    ,document_date as document_date_3 format=year4.
  from have
  ;
quit;

Patrick_0-1714640757859.png

 

Kurt_Bremser
Super User

Both the YEAR function and the YEAR format expect an input value which represents a SAS date. SAS stores dates as counts of days, with 1960-01-01 being day zero.

Since the result of the YEAR function is not a SAS date, but the year number, applying the DATE format to the result will interpret this number as a count of days and come up with a year of 1965 for the value of 2024.

For the display of a year value like the number 2024, no format is needed. It's just a 4-digit number, unless you reached so far into the future that you went past 9999.

user34567
Obsidian | Level 7

Hi @Kurt_Bremser,

Thank you for your explanation. I've set the format to 'none' and now it indeed works!

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 3 replies
  • 325 views
  • 0 likes
  • 3 in conversation