DATA Step, Macro, Functions and more

extract year from a character variable

Accepted Solution Solved
Reply
Regular Contributor
Posts: 196
Accepted Solution

extract year from a character variable

I have a character variable with the following data:
9/6/2013 11:16:13 AM
8/2/1994 4:18:06 PM
11/15/1995 3:54:36 PM

How would I go about extracting the four-digit year (eg 2013)?

Thank you.


Accepted Solutions
Solution
‎10-16-2013 01:10 PM
Super User
Posts: 17,907

Re: extract year from a character variable

Try using the scan function with a space and / as delimiter.

year=scan(var, 3, "/", 's');

Documentation on the SCAN function is here:

SAS(R) 9.2 Language Reference: Dictionary, Fourth Edition

View solution in original post


All Replies
Solution
‎10-16-2013 01:10 PM
Super User
Posts: 17,907

Re: extract year from a character variable

Try using the scan function with a space and / as delimiter.

year=scan(var, 3, "/", 's');

Documentation on the SCAN function is here:

SAS(R) 9.2 Language Reference: Dictionary, Fourth Edition

Respected Advisor
Posts: 3,124

Re: extract year from a character variable

Or try this:

data _null_;

input var$40.;

year=year(input(var,ANYDTDTE32.));

put year=;

cards;

9/6/2013 11:16:13 AM

8/2/1994 4:18:06 PM

11/15/1995 3:54:36 PM

;

Haikuo

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 236 views
  • 3 likes
  • 3 in conversation