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

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
SASKiwi
PROC Star

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

6 REPLIES 6
SASKiwi
PROC Star

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

 

ali_far
Obsidian | Level 7

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

SASKiwi
PROC Star

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

ali_far
Obsidian | Level 7

many thanks ir worked well.

ali_far
Obsidian | Level 7

thanks it is solved

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 6 replies
  • 2362 views
  • 0 likes
  • 3 in conversation