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!

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

Register now!

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 724 views
  • 0 likes
  • 3 in conversation