BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
gsk
Obsidian | Level 7 gsk
Obsidian | Level 7

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. 

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

5 REPLIES 5
Reeza
Super User

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. 


 

ballardw
Super User

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

gsk
Obsidian | Level 7 gsk
Obsidian | Level 7

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 DatasetOriginal DatasetDataset with WHERE statementDataset with WHERE statementthe log with WHERE statementthe log with WHERE statement

 

Reeza
Super User

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;

Astounding
PROC Star

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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