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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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