BookmarkSubscribeRSS Feed
Quartz | Level 8

I'm trying to recode age into groups but this is not working (just takes the first number in the bracket, from what I can tell)


data new;

set have;


if age in (15-24) then  agegroup=1;

else if age in (25-54) then agegroup=2;

else if age >= 55 then agegroup=3;




What is the proper way to do this?



Edit: Also I know one can do it the way below, but it is very tedious, especially when there are more than 3 values being made. I'm looking for something easier, with ranges:


IF       15      <= age_tabs <=  24      then     agegroup_main = 1;      
else if  25      <= age_tabs <=  54      then     agegroup_main =2;      
else if             age_tabs >=   55     then     agegroup_main =3;      
else                agegroup_main = .
Super User

One way is to not add any variables an use your existing data set and assign a format to the variable.


proc format;

value agegrp

1 - 24 = '1'

25 - 54= '2'

55 - high='3';



proc freq data=have;

   tables age;

   format age agegrp.;



The format will create the group for the counts as well as changing displayed value.


Your code is not correct, use the colon : to indicate a numeric range with the IN operator

if age in (15:24) then  agegroup=1;

else if age in (25:54) then agegroup=2;

else if age >= 55 then agegroup=3;



Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.


Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 1 reply
  • 2 in conversation