Solved
Regular Learner
Posts: 1

# use range as a variable

Hi, this might sound stupid, but i have been looking for the answer for last few hours, but I still can't figure this out.

The following code shows how I define my 'agegroup' variable:

``````if i in (1, 5) then agegroup = '<30';
else if i in (2, 6) then agegroup = '31-45';
else if i in (3, 7) then agegroup = '46-60';
else agegroup = '60+';``````

But my sas output eliminates the number after the dash:

I thought this might be a problem about variable type, but I am not sure anymore. I also tried to replace the single quote with double quote, but it isn't working.

Accepted Solutions
Solution
‎10-16-2017 03:48 PM
Valued Guide
Posts: 596

## Re: use range as a variable

If you don't specify a length for a character variable it takes as its length the length of the first value assigned to it. In your case there must be a value of either 1 or 5 as the first value in the data so agegroup has a length of 3 ('<30').

You can correct this by adding

``length agegroup  \$5;``

All Replies
Solution
‎10-16-2017 03:48 PM
Valued Guide
Posts: 596

## Re: use range as a variable

If you don't specify a length for a character variable it takes as its length the length of the first value assigned to it. In your case there must be a value of either 1 or 5 as the first value in the data so agegroup has a length of 3 ('<30').

You can correct this by adding

``length agegroup  \$5;``

Posts: 4,797

## Re: use range as a variable

@ChrisBrooks

"In your case there must be a value of either 1 or 5 as the first value in the data so agegroup has a length of 3 "

Variables get defined during the compilation phase and before data step iteration so what's in the data doesn't matter. The full reason for a length of 3 is, that this is the length of the first value assignment in the code:   then agegroup = '<30'

Super Contributor
Posts: 359

## Re: use range as a variable

@ChrisBrooks gave you the explanation to your problem.

Instead of creating a new variable, you can use a format :

``````proc format;
value agegroup
1,5='<30'
2,6='31-45'
3,7='46-60'
other='60+'
;
run;

proc print data=have;
format i agegroup.;
run;``````
☑ This topic is solved.

Discussion stats
• 3 replies
• 161 views
• 7 likes
• 4 in conversation