BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
MisterJenn
Fluorite | Level 6

I am new to SAS. I am trying to figure out the best way to create a new variable with the following directions: 

 

Create a new variable called age_bmicat according to the specifications in the table below:

Screen Shot 2022-11-23 at 4.15.46 PM.png

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

In a SAS data step

 

if age<=65 and (bmi<18 or bmi>25) then age_bmicat=1;

 

 

Using that as an example, see if you can figure out the code for the rest of the problem.

--
Paige Miller

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26

In a SAS data step

 

if age<=65 and (bmi<18 or bmi>25) then age_bmicat=1;

 

 

Using that as an example, see if you can figure out the code for the rest of the problem.

--
Paige Miller
Reeza
Super User
data want;
set have;

if age <= 65 then do;
    if  18 <= bmi <= 25 then age_bmicat = 2;
    else if age_bmicat < 18 or age_bmicat > 25 = 1;
end;
else if age > 65 then do;
    if  25 <= bmi <= 30 then age_bmicat =  4;
     else if age_bmicat < 25 or age_bmicat > 30 = 3;
end;

run;

@MisterJenn wrote:

I am new to SAS. I am trying to figure out the best way to create a new variable with the following directions: 

 

Create a new variable called age_bmicat according to the specifications in the table below:

Screen Shot 2022-11-23 at 4.15.46 PM.png


 

tarheel13
Rhodochrosite | Level 12

haven't seen your data but you should do this check anyway: 

tarheel13_0-1669485957708.png

data bmi;
   set prac.chol_new;
   if nmiss(ht,wt)=0 then do;
      bmi=703*(wt/(ht**2));
   end;
   
   if nmiss(age,bmi)=0 then do;
      if age le 65 then do;
         if bmi < 18 or bmi > 25 then age_bmicat=1;
         else age_bmicat=2;
      end;
      else do;
         if bmi < 25 or bmi > 30 then age_bmicat=3;
         else age_bmicat=4;
      end;
   end;
run;

proc means data=bmi n nmiss min max;
   class age_bmicat / missing;
   var age bmi;
run;

Note, it is important to only create the new categorical variable when age and BMI are both nonmissing.

 

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!

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
  • 3 replies
  • 509 views
  • 1 like
  • 4 in conversation