DATA Step, Macro, Functions and more

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

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 13
Accepted Solution

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 ?

Posted in reply to vishalrajpoot3
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.

View solution in original post


All Replies
Respected Advisor
Posts: 3,261

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

[ Edited ]
Posted in reply to vishalrajpoot3

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 ?

Posted in reply to vishalrajpoot3

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 ?

Posted in reply to Astounding

 

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 ?

Posted in reply to vishalrajpoot3

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 ?

Posted in reply to vishalrajpoot3
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.

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

Discussion stats
  • 5 replies
  • 90 views
  • 0 likes
  • 5 in conversation