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

Hello, I need help with coding. I am trying to categorize a numeric (Age). I went to categorize it by 1-17 is Children/Adolescent 18-39 is young adult 40-55 is middle age adult and 56 and up is older adult

1 ACCEPTED SOLUTION

Accepted Solutions
sustagens
Pyrite | Level 9

You can use if-then within your code like this

data want;
set have;

format category $30.;

if 1 <= age <= 17 then category="Children/Adolescent";
else if 18 <= age <=39 then category="Young Adult";
else if 40<= age <= 55 then category="Middle Age Adult";
else if age >= 56 then category="Older Adult";

run;

 

Or use a format like this

  proc format;
  value age 
  1-17='Children/Adolescent'
  18-39='Young Adult'
  40-55='Middle Age Adult'
  other='Older Adult'
  ;

  data;
  input name $ age;
  format age age.;
  cards;
  cherry 14
  maria 28
  patty 45
  chris 57
  josh 2
  ;

View solution in original post

2 REPLIES 2
sustagens
Pyrite | Level 9

You can use if-then within your code like this

data want;
set have;

format category $30.;

if 1 <= age <= 17 then category="Children/Adolescent";
else if 18 <= age <=39 then category="Young Adult";
else if 40<= age <= 55 then category="Middle Age Adult";
else if age >= 56 then category="Older Adult";

run;

 

Or use a format like this

  proc format;
  value age 
  1-17='Children/Adolescent'
  18-39='Young Adult'
  40-55='Middle Age Adult'
  other='Older Adult'
  ;

  data;
  input name $ age;
  format age age.;
  cards;
  cherry 14
  maria 28
  patty 45
  chris 57
  josh 2
  ;
ballardw
Super User

As an expansion of @sustagens good solution:

 

The format approach is often the most flexible. If you add variables then when someone asks a question like:

What if the children are broken out at age < 13 and 13 to 17? Or other age groups for the adults? Then you have to go back to the data step, create another variable and rerun everything. And then do it again when they ask yet another age grouping.  I have had as many as 18 different age-based formats at one time as different projects have different reporting boundaries for example.

 

The groups created by formats in this fashion will work with most of the analysis (I say most because I haven't run every single analysis proc), reporting and graphing procedures. So to add a category you 1) create the new format and 2) apply in the procedure call(s).

And for a few procedures (Tabulate, Report, Means, Summary) there is an option call MULTILABEL format that would do a summary by 1-<13, 13-17 and 1-17 at the same time.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 2 replies
  • 722 views
  • 2 likes
  • 3 in conversation