Hi robm,
Year function will work to extract year from datetime/date variable:
data want;
format a datetime.;
a=0;
yr=year(a);
run;
I think u r variable might be in character format.Check once.
Cheers,
Karthik
Karthik,
The argument of the Year function is a date (not a datetime) so the correct use of the function would be as follows:
data want;
format a datetime.;
a=datetime();
yr=year(datepart(a));
run;
Regards,
use san function
scan(do_admn_dt,1,': ')
scan(do_admn_dt,1,': ')
try this
do_admn_dt_year =input(substr(scan(compress(do_admn_dt),1,': '),6,4),9.2);
You're missing that this is about extracting the year part out of a SAS datetime value - which is stored as a numeric value and not a character value. "Substr()", "scan()" and "compress()" are functions for character values dealing with strings.
To extract the year out of the numeric variable "dt" containing as SAS datetime value you can either use something like "year(datepart(dt))" which will return the year as a four digit numeric value, or you can use something like "put(dt,dtyear4.)" which will return the year as a four digit string (character value).
sorry Patrick
you posted while I was searching for a DTYEAR function
Don't think my post adds anything.
regards
peter
why does this seem so difficult?
If the original var is string then the substr() would NOT have caused the error message.
since the numeric value has the presentation style of a datetime value I assume the internal value is a datetime.
to create a string from a numeric like this we use a put() function. The function uses a format to choose the presentation style. The format to present just the year of a datetime value is DTYEAR.
YEAR_STRING= PUT( D_O_ADMN_DT, DTYEAR4. ) ;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.