BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
mingy
Calcite | Level 5

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. 

1 ACCEPTED SOLUTION

Accepted Solutions
ChrisBrooks
Ammonite | Level 13

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

3 REPLIES 3
ChrisBrooks
Ammonite | Level 13

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

Patrick
Opal | Level 21

@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'

gamotte
Rhodochrosite | Level 12

@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;
How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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