Solved
Contributor
Posts: 25

# Setting ranges of a variable

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.

Accepted Solutions
Solution
‎02-15-2018 04:30 PM
Super User
Posts: 6,934

## Re: Setting ranges of a variable

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)) ;

All Replies
Super User
Posts: 24,012

## Re: Setting ranges of a variable

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.

Super User
Posts: 13,941

## Re: Setting ranges of a variable

"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.

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.

Contributor
Posts: 25

## Re: Setting ranges of a variable

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

Original DatasetDataset with WHERE statementthe log with WHERE statement

Super User
Posts: 24,012

## Re: Setting ranges of a variable

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;

``````
Solution
‎02-15-2018 04:30 PM
Super User
Posts: 6,934

## Re: Setting ranges of a variable

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)) ;

☑ This topic is solved.