I have 4 variables of interest: weight, weight unit, height, height unit.
Weight unit variable has two possible values, pounds or kg.
Height unit variable has two possible values, inch or cm.
I want to write a code in a data step that does this - how do we write such code in order to eliminate/print out abnormal values?
when weight unit is in pound & weight is/is not within a to b
when weight unit is in kg & weight is/is not within c to d
when height unit is in inch & height is/is not within a2 to b2
when height unit is in cm & height is/is not within c2 to d2
I tried a bunch of if/where statements similar to this:
data a;
set dem;
if not (dema6b = "K" & 30 <=dema6a<=250) or
(dema6b = "P" & 66<=dema6a<=550) or
(dema6d = "C" & 121<=dema6c<=229) or
(dema6d = "I" & 4<=dema6c<=7.5);
run;
but they didn't do the job.
I think this is the combination you are after:
if (dema6b = "K" & not (30 <=dema6a<=250)) or
(dema6b = "P" & not (66<=dema6a<=550)) or
(dema6d = "C" & not (121<=dema6c<=229)) or
(dema6d = "I" & not (4<=dema6c<=7.5)) ;
but they didn't do the job.
Your code looks fine so what does 'didn't do the job' mean?
@gsk wrote:
I have 4 variables of interest: weight, weight unit, height, height unit.
Weight unit variable has two possible values, pounds or kg.
Height unit variable has two possible values, inch or cm.
I want to write a code in a data step that does this - how do we write such code in order to eliminate/print out abnormal values?
when weight unit is in pound & weight is/is not within a to b
when weight unit is in kg & weight is/is not within c to d
when height unit is in inch & height is/is not within a2 to b2
when height unit is in cm & height is/is not within c2 to d2
I tried a bunch of if/where statements similar to this:
data a;
set dem;
if not (dema6b = "K" & 30 <=dema6a<=250) or
(dema6b = "P" & 66<=dema6a<=550) or
(dema6d = "C" & 121<=dema6c<=229) or
(dema6d = "I" & 4<=dema6c<=7.5);
run;
but they didn't do the job.
"Didn't do the job" is awful vague.
Are there errors in the log?: Post the code and log in a code box opened with the {i} to maintain formatting of error messages.
No output? Post any log in a code box.
Unexpected output? Provide input data in the form of a dataset, the actual results and the expected results. Data should be in the form of a data step. Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.
I'm sorry! So with and without the if/where statement, it has same number of observations (1887).
My if/where statements are not doing anything.
Here are screenshots of 1) proc print result of the original dataset 2) proc print result of the dataset with the codes I wrote on the question (except that I changed if to where) 3) the log output with the subsetting codes
We can't read that and I'm not even going to try.
Change your conditions from subsetting to Flags.
ie create a flag for the first, second and third conditions and run a PROC FREQ on the flags. Or filter on the results by the flag and examine it. It's not a coding issues, your logic is incorrect and we can't debug your logic if we don't know what you want.
data want;
set have;
if not (dema6b = "K" and 30 <=dema6a <= 250 ) then flag1=1;
else flag1=0;
run;
data check;
set want;
where flag1=1;
run;
I think this is the combination you are after:
if (dema6b = "K" & not (30 <=dema6a<=250)) or
(dema6b = "P" & not (66<=dema6a<=550)) or
(dema6d = "C" & not (121<=dema6c<=229)) or
(dema6d = "I" & not (4<=dema6c<=7.5)) ;
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!
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.