DATA Step, Macro, Functions and more

extract the year from the data variable

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 9
Accepted Solution

extract the year from the data variable

Hi every one,

I have tried many ways to extrat the year from the date variable of my data set.however. non of them works(substr statement etc)

i even change the format of variable.

note: the format of the varibale in the manual book is shown : Text (7) (YYYY-MM)

variable                                                     what i want

data_of_brith                                                    year

01APR24:00:00:00                                             24

01AUG25:00:00:00                                            25

01DEC32:00:00:00                                            32

...

my code:

data want;
set have;
year = input(substr(data_of_brith, 6), 2.);
run;

the result are: 82, 61, 46 instead of 24, 25, 32 that i was expecting!!!!!

your hepl will be really appreciated

 


Accepted Solutions
Solution
‎07-08-2017 03:45 AM
Super User
Posts: 3,111

Re: extract the year from the data variable

[ Edited ]

Use the YEAR function which includes century as well to avoid confusion. Your post isn't clear but it looks like your birthdate variable is a datetime. If so you need to add DATEPART:

 

year = year(datepart(data_of_brith)); 

 

View solution in original post


All Replies
Solution
‎07-08-2017 03:45 AM
Super User
Posts: 3,111

Re: extract the year from the data variable

[ Edited ]

Use the YEAR function which includes century as well to avoid confusion. Your post isn't clear but it looks like your birthdate variable is a datetime. If so you need to add DATEPART:

 

year = year(datepart(data_of_brith)); 

 

Occasional Contributor
Posts: 9

Re: extract the year from the data variable

Thanks for your reply , but it tried before this and it did not work. it gave me null for the whole column...

Super User
Posts: 3,111

Re: extract the year from the data variable

See my changed post adding DATEPART. Please confirm if your variable is in DATETIME or DATE format.

Occasional Contributor
Posts: 9

Re: extract the year from the data variable

many thanks ir worked well.

Super User
Posts: 6,946

Re: extract the year from the data variable

Post the log of the failed step.
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor
Posts: 9

Re: extract the year from the data variable

thanks it is solved

☑ This topic is SOLVED.

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

Discussion stats
  • 6 replies
  • 147 views
  • 0 likes
  • 3 in conversation