Write and run SAS programs in your web browser

grouping

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

grouping

I am with SAS studio.

I have a sas7data file with a large table with different subjects as the independant variable and their anthropometric data (weight, age, BMI, etc) as dependant variables.

I am trying to sort this data by BMI (for body mass index) in order to creat groups of people having a BMI < or = to 25, >25 to <30 and > or = 30. How do I do this?

 

Thank you!


Accepted Solutions
Solution
‎05-06-2016 11:22 AM
Regular Contributor
Posts: 169

Re: grouping

Great question! Do you want separate data sets for each group?
data lowBMI mediumBMI highBMI error;
    set originalDataSet;
    select; 
        when (BMI <= 25) output lowBMI; 
        when (BMI < 30) output mediumBMI; 
        when (BMI >= 30) output highBMI; 
        otherwise output error;
    end; 
run;
If you want to keep them in the same data set but add a variable with category, you can instead of "output dataSet" say BMIcategory = "low", for example.

Another solution is a user-defined format. There's a good tutorial here if you'd prefer that.

View solution in original post


All Replies
Solution
‎05-06-2016 11:22 AM
Regular Contributor
Posts: 169

Re: grouping

Great question! Do you want separate data sets for each group?
data lowBMI mediumBMI highBMI error;
    set originalDataSet;
    select; 
        when (BMI <= 25) output lowBMI; 
        when (BMI < 30) output mediumBMI; 
        when (BMI >= 30) output highBMI; 
        otherwise output error;
    end; 
run;
If you want to keep them in the same data set but add a variable with category, you can instead of "output dataSet" say BMIcategory = "low", for example.

Another solution is a user-defined format. There's a good tutorial here if you'd prefer that.
New Contributor
Posts: 3

Re: grouping

Thank you sooo much,

now I need to analyze the means of different variables of these groups. This following procedure says that this "proc means; by lowIMC mediumIMC highIMC error" are not valid groups.... what should I do....


data lowIMC mediumIMC highIMC error;
    set BIOMRKR;
    select;
        when (IMC <= 25) output lowIMC;
        when (IMC < 30) output mediumIMC;
        when (IMC >= 30) output highIMC;
        otherwise output error;
    end;
run;

proc means; by lowIMC mediumIMC highIMC error; var fruit legume f_l dietacar dietbcar bcryp lycopene lutzea;run;

proc corr;var fruit dietacar; run;

proc corr; var fruit dietbcar;run;

Regular Contributor
Posts: 169

Re: grouping

Well if you do it like that, lowIMC, mediumIMC, highIMC, and error are different data sets. So use a separate PROC MEANS for each.

Super User
Posts: 11,101

Re: grouping

It may be much easier in the long run to use Paulkaefer's idea of adding a variable to represent the categories.

Please see:

data want;
    set originalDataSet;
    select; 
        when (BMI <= 25) BmiCat='Low'; 
        when (BMI < 30) BmiCat='Medium'; 
        when (BMI >= 30) BmiCat='Obese'; 
        otherwise ;
    end; 
run;

proc means data=want;
   class BmiCat;
   var fruit legume f_l dietacar dietbcar bcryp lycopene lutzea;
run;

proc sort data=want;
   by BmiCat;
run;

proc corr data=want;
   by BmiCat;
   var Fruit dietacar;
run;
☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 384 views
  • 0 likes
  • 3 in conversation