BookmarkSubscribeRSS Feed
SChander1
Calcite | Level 5

Hello everyone,

 

how can I combine multiple age people into two groups?? Lower than 65 and above 65?

 

Thanks

6 REPLIES 6
PeterClemmensen
Tourmaline | Level 20
data want;
   set have;
   if age>65 then AgeGroup='Above 65';
   else AgeGroup='Below 65';
run;
SChander1
Calcite | Level 5

this code shows me error. I have used

data xyz;

set tyy;

if age <= 65 then age1= 1;

if age > 65 then age1 = 0;

run;

 

and its working.. thanks for your prompt response.

 

 

VDD
Ammonite | Level 13 VDD
Ammonite | Level 13

well the request did not say which group age 65 belonged to.

The original requested says lower that 65 and above 65.

so both are correct in the fact that the request was not questioned due to undefined requirements for which group age 65 belonged to.

 

 

Reeza
Super User

You should consider using IF and ELSE, instead of two IF statements. 


This way, if one condition is true, the second will not be tested and its more efficient. This also means that you're less likely to accidentally overwrite your values later on when you get into more complex IF/THEN logic. 

 

if age <= 65 then age1= 1;
else if age > 65 then age1 = 0;

@SChander1 wrote:

this code shows me error. I have used

data xyz;

set tyy;

if age <= 65 then age1= 1;

if age > 65 then age1 = 0;

run;

 

and its working.. thanks for your prompt response.

 

 


 

andreas_lds
Jade | Level 19

@SChander1 wrote:

this code shows me error. I have used

data xyz;

set tyy;

if age <= 65 then age1= 1;

if age > 65 then age1 = 0;

run;

 

and its working.. thanks for your prompt response.

 

 


Please post the error-message you got. The code posted by @PeterClemmensen seems to be error-free.

ballardw
Super User

@SChander1 wrote:

Hello everyone,

 

how can I combine multiple age people into two groups?? Lower than 65 and above 65?

 

Thanks


90% or time or more when I need such, grouping on a single value, I use a custom format. Reasons: I can change or use a different format and do not need to change the data set at all, the code for ensuring non-overlapping ranges in the format code is usually much simpler.

Example:

/* create a data set with some age groups*/
data example;
   do age = 1 to 85;
      do j= 1 to rand('integer',2,6);
      output;
      end;
   end;
run;

proc format library = work;
value age65_
1 - 65 = '65<'
65<-high = '65+'
;
value age10yr
1 -10= ' 1-10'
11-20= '11-20'
21-30= '21-30'
31-40= '31-40'
41-50= '41-50'
51-60= '51-60'
61-70= '61-70'
71-80= '71-80'
81-high='81+'
;

proc freq data=example;
   tables age;
   format age age65_.;
run;
proc freq data=example;
   tables age;
   format age age10yr.;
run;

The first data set is to build a data set with a number of age values. Then two formats to group age. I actually have more than a dozen of such with other values such as "out of range" because my users have different boundary ages for their processes.

Note that a format definition name cannot end in a numeral because formats usage allows specifying the number of display characters at run time such as:

proc freq data=example;
   tables age;
   format age age10yr2.;
run;

which shows the lower bound of the age group only.

 

Almost all of the analysis, graphing and reporting procedures will support groups created by a format.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 6 replies
  • 1834 views
  • 5 likes
  • 6 in conversation