Contributor
Posts: 26

# SAS BASE- Range conversion

Hi,

I have a variable "Age" with Data as;

18-24

15-30

29-54

The variable is Character. How can I convert the range to numeric to calculate age range ie. 18-24 is 6 years.

Super User
Posts: 6,751

## Re: SAS BASE- Range conversion

[ Edited ]
Posted in reply to SASEnthusiast

One way:

age_range = input(scan(age,2,'-'), 3.) - input(scan(age,1,'-'), 3.);

You might want to add 1 to the result, if you decide that 18-24 actually spans 7 years not 6.

Contributor
Posts: 21

## Re: SAS BASE- Range conversion

Posted in reply to SASEnthusiast

data new_age;

set test;

new_age =(INPUT(SCAN(Age, -1), 8.))-(INPUT(SCAN(Age, 1), 8.));

run;

Super User
Posts: 10,766

## Re: SAS BASE- Range conversion

Posted in reply to SASEnthusiast
``````data have;
input age \$20.;
dif=-1*input(resolve(cats('%sysevalf(',age,')')),best.);
cards;
18-24
15-30
29-54
;
run;``````
Posts: 5,521

## Re: SAS BASE- Range conversion

Very funny @Ksharp!

PG
Super User
Posts: 10,766

## Re: SAS BASE- Range conversion

Yeah. you can do it with any algorithmic expression.

``````data have;
input age \$20.;
dif=input(resolve(cats('%sysevalf(',age,')')),best.);
cards;
18-24
15*30
29/54
;
run;``````
Discussion stats
• 5 replies
• 264 views
• 2 likes
• 5 in conversation