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

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

CTorres
Quartz | Level 8

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,

TarunKumar
Pyrite | Level 9

use san function

scan(do_admn_dt,1,': ')

Patrick
Opal | Level 21

"scan()" is like "substr()" a string function and you would face the same issues as described in the initial post.

TarunKumar
Pyrite | Level 9

scan(do_admn_dt,1,': ')

try this

do_admn_dt_year =input(substr(scan(compress(do_admn_dt),1,': '),6,4),9.2);

Patrick
Opal | Level 21

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).

Peter_C
Rhodochrosite | Level 12

sorry Patrick

you posted while I was searching for a DTYEAR function

Don't think my post adds anything.

regards

peter

Peter_C
Rhodochrosite | Level 12

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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

What is Bayesian Analysis?

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 22 replies
  • 31098 views
  • 8 likes
  • 7 in conversation