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

How do I pull the 4 digit year out of this 25MAY2015:00:00:00  in SAS?

I want to create a column year with the year of the transaction

I tried several different combos...

      format      year  YEAR4.;

      year = substr(transact_date,5,4); 

Then I changed year to transact_year

23OCT2018:00:00:00.00000  is the transact_date in the results and it gives me 1975 as the year

Any ideas?

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

SAS stores dates as number of days and datetimes as number of seconds.

 

The YEAR format is for DATE values, not DATETIME values.  If you try to use it with a date value well into the future since there a 86,400 seconds in a single day.

The SUBSTR() function is for character values, not numeric values.  If you try to apply it to a numeric value SAS will first convert the number to a string using the BEST12. format.

 

If you convert the datetime value to a datevalue you can use the YEAR format to display it as the year number.  Or you could use the YEAR() function to extract the year as a number in years.

data want;
  set have (rename=(transact_date=transact_datetime));
  transact_date=datepart(transact_datetime);
  transact_year=year(transact_date);
  format transact_date date9.;
run;

View solution in original post

7 REPLIES 7
novinosrin
Tourmaline | Level 20

Assuming your datetime value is numeric,

 year = year(datepart(transact_date)); 

 

You could also consider just formatting the datetime numeric value of transact_date to display only the year values by using dtyear format

format  transact_date dtyear.;
bweeks59
Obsidian | Level 7
Thanks for the quick reply, I will try the methods you have listed and let you know how it goes.
novinosrin
Tourmaline | Level 20

And if your datetime value is char, then

year=year(input(transact_date,date9.));
bweeks59
Obsidian | Level 7
Yes the date is a SAS datetime value, I am working on a few methods to see which one produces the required result and will let you know if i have success:-) Thanks a bunch
Tom
Super User Tom
Super User

SAS stores dates as number of days and datetimes as number of seconds.

 

The YEAR format is for DATE values, not DATETIME values.  If you try to use it with a date value well into the future since there a 86,400 seconds in a single day.

The SUBSTR() function is for character values, not numeric values.  If you try to apply it to a numeric value SAS will first convert the number to a string using the BEST12. format.

 

If you convert the datetime value to a datevalue you can use the YEAR format to display it as the year number.  Or you could use the YEAR() function to extract the year as a number in years.

data want;
  set have (rename=(transact_date=transact_datetime));
  transact_date=datepart(transact_datetime);
  transact_year=year(transact_date);
  format transact_date date9.;
run;
bweeks59
Obsidian | Level 7

This worked and fast too:-) thanks so much for everyone's help....

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!
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
  • 7 replies
  • 658 views
  • 4 likes
  • 4 in conversation