Hello, I use the WHERE to set my data, but the log shows the error like below:
And I'm wondering what's goes wrong.
Thank you.
proc import datafile='/home/u63518324/sasuser.v94/Epi_test.xls'
out=homework_per
dbms=xls;
getnames=yes;
run;
data homework_1;set homework_per;
BMI=weight/(height*height)*10000;
if BMI<24 then BMI_group=1;
else if BMI>=24 then BMI_group=2;
run;
proc format;
value BMI_group 1='normal weight' 2='overweight';
run;
data homework_1;set homework_1;
format BMI_group BMI_group.;
run;
proc print data=homework_1 AND BMI_group='overweight';
where sex='F';
run;
proc print data=homework_1;
where BMI_group='overweight';
run;
ERROR: WHERE clause operator requires compatible variables.
You need to amend your data step so variable bmi_group gets populated.
data homework_2;
set homework_1;
bmi_group=put(bmi,bmi_group.);
run;
For just using a formatted value in a where clause you could also directly write code like:
proc print data=homework_1;
where put(bmi,BMI_group.)='overweight';
run;
And if you then want to print the bmi values with a format applied you could code like:
proc print data=homework_1;
where put(bmi,BMI_group.)='overweight';
format bmi bmi_group.;
run;
Assuming that "and BMI_group ..." belongs to the where statement.
proc print data=homework_1;
where sex='F' AND BMI_group='overweight';
run;
Attaching a format to a variable does not change the type of the variable, only the way its values are displayed changes. So BMI_group is still numeric and the comparison with "overweight" causes the error.
You need to amend your data step so variable bmi_group gets populated.
data homework_2;
set homework_1;
bmi_group=put(bmi,bmi_group.);
run;
For just using a formatted value in a where clause you could also directly write code like:
proc print data=homework_1;
where put(bmi,BMI_group.)='overweight';
run;
And if you then want to print the bmi values with a format applied you could code like:
proc print data=homework_1;
where put(bmi,BMI_group.)='overweight';
format bmi bmi_group.;
run;
I follow your steps and solve the problem!
Thank you so much!!!!!!
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!
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.