## Using Length and Substr function only how can i extract age from the data ?

Solved
Occasional Contributor
Posts: 13

# Using Length and Substr function only how can i extract age from the data ?

Will someone provide me the solution for subjected problem with the below data, I want to extract age from the below data using 'Length' and 'Substr' function.

```Data test;
input nameage \$10.;
datalines;
Ram24
Albertaa26
Bob85
AB23
;
run;```

Accepted Solutions
Solution
a month ago
Occasional Contributor
Posts: 16

## Re: Using Length and Substr function only how can i extract age from the data ?

Hi Vishal,

Age=input(substr(nameage,length(nameage)-1),8.);

In this way you can use substr and length function both.If you are sure about that age is coming as a last 2 digit.

All Replies
Posts: 3,261

## Re: Using Length and Substr function only how can i extract age from the data ?

[ Edited ]

Show us the code you have already tried.

--
Paige Miller
Super User
Posts: 6,928

## Re: Using Length and Substr function only how can i extract age from the data ?

Do you want the extracted age to be character or numeric?

Are you being forced to use the tools you mentioned?  They're not the best ones for the job.  Using those only might force you to assume that the age is always two digits long.

Occasional Contributor
Posts: 13

## Re: Using Length and Substr function only how can i extract age from the data ?

I want age in numeric, yes only for this scenario age is only two digit long.

Super User
Posts: 10,555

## Re: Using Length and Substr function only how can i extract age from the data ?

length() is not needed and actually dangerous (think of kids and centenarians).

Use anydigit() to find the first occurence of a digit, and substr() from there.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Solution
a month ago
Occasional Contributor
Posts: 16

## Re: Using Length and Substr function only how can i extract age from the data ?

Hi Vishal,

Age=input(substr(nameage,length(nameage)-1),8.);

In this way you can use substr and length function both.If you are sure about that age is coming as a last 2 digit.
☑ This topic is solved.