DATA Step, Macro, Functions and more

Setting ranges of a variable

Accepted Solution Solved
Reply
Contributor gsk
Contributor
Posts: 23
Accepted Solution

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,544

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

View solution in original post


All Replies
Super User
Posts: 22,875

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,084

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.

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.

Contributor gsk
Contributor
Posts: 23

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

 

dem_data.JPGOriginal DatasetAfterwhere_1887_obs.JPGDataset with WHERE statementlog.JPGthe log with WHERE statement

 

Super User
Posts: 22,875

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,544

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.

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

Discussion stats
  • 5 replies
  • 171 views
  • 1 like
  • 4 in conversation