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?
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;
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.;
And if your datetime value is char, then
year=year(input(transact_date,date9.));
Or
year = input(put(transact_date,dtyear4.),4.);
if you have a SAS datetime value.
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;
This worked and fast too:-) thanks so much for everyone's help....
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.