DATA Step, Macro, Functions and more

use range as a variable

Accepted Solution Solved
Reply
Regular Learner
Posts: 1
Accepted Solution

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:

2.PNG

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: 561

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;

before your set statement

View solution in original post


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

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;

before your set statement

Respected Advisor
Posts: 4,557

Re: use range as a variable

Posted in reply to ChrisBrooks

@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: 320

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.

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

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