BookmarkSubscribeRSS Feed
mmea
Quartz | Level 8

Hi

 

if I have these to variables in a data set

 

variable1 variable2
0.90 300
0.20 65
0.01 400

 

I need to make a variable3 based on following statement:

 

if variable1 >= 0.1 and/OR variable2  >= 200 then variable3 = "good"

if variable1 < 0.1 and/or variable2 <200 then variable3 = "bad"

 

So in this case variable3 will be

good

good

good

11 REPLIES 11
PaigeMiller
Diamond | Level 26

The only logic I know of does not recognize "AND/OR", its more of a figure of speech than any logical or programming concept. So I can only guess what you mean by

 

if variable1 >= 0.1 and/OR variable2  >= 200 then variable3 = "good"

if variable1 < 0.1 and/or variable2 <200 then variable3 = "bad"

 

Isn't this the same as using OR in place of AND/OR in the first IF? If not, explain why.

 

(And side issue, please please please put semi-colons at the end of your SAS statements)

--
Paige Miller
Kurt_Bremser
Super User

Please add cases that should end up as "bad".

Do it in this form:

data have;
input variable1 variable2;
datalines;
0.90  300
0.20  65
0.01  400
;

so we can quickly create the dataset for testing with copy/paste and submit.

mmea
Quartz | Level 8
data have;
input variable1 variable2;
datalines;
0.90  300
0.20  65
0.01  400
0.006 100
0.67  78
;

So in this case

0.90 (>0.10) and 300 (>200) is good

0.20 (>0.10) and 65 (<200) is good

0.01 (<0.10) and 400 (>200) is good

0.006 (<0.10) and 100 (<200) is bad

0.67 (>0.10) and 78 (<200) is bad

 

mmea
Quartz | Level 8

Sorry its should be good - my mistake

 

So if one of them have the criteria >10% or >=200 then its good no matter what the other are 

Kurt_Bremser
Super User
data have;
input variable1 variable2;
datalines;
0.90  300
0.20  65
0.01  400
0.006 100
0.67  78
;

data want;
set have;
if variable1 gt 0.1 or variable2 ge 200
then result = "good";
else result = "bad";
run;
mmea
Quartz | Level 8

but will this consider when variable1 is below 0.1 but variable2 is GE 200 as TRUE?

 

AMSAS
SAS Super FREQ

I'll add to the comments, what happens when variable1=0.2 and variable2=199 is that good (variable1>0.1) or bad (variable2<200)

 

You could find it helpful to build a table:

Variable 1 Value (>0.1) Variable 2 Value (>200) Result
0.9 (True) 300 (True) good (True)
0.05 (False) 300 (True) ????
0.9 (True) 100 (False) ????
0.05 (False) 100 (False) bad (False)

 

mmea
Quartz | Level 8

Yes that is a good because variable 1 is over 10% percent - as long one of thm match the criteria >10% or >=200

 

Variable 1 Value (>0.1) Variable 2 Value (>200) Result
0.9 (True) 300 (True) good (True)
0.05 (False) 300 (True) good
0.9 (True) 100 (False) good
0.05 (False) 100 (False) bad (False)

 

It wil be like that

PaigeMiller
Diamond | Level 26

@mmea wrote:

Yes that is a good because variable 1 is over 10% percent - as long one of thm match the criteria >10% or >=200

 

Variable 1 Value (>0.1) Variable 2 Value (>200) Result
0.9 (True) 300 (True) good (True)
0.05 (False) 300 (True) good
0.9 (True) 100 (False) good
0.05 (False) 100 (False) bad (False)

 

It wil be like that


So when both variable 1 and variable 2 return a "False" then the result is "False". Any other condition returns a "True".

 

Then you want when 

 

if variable1<=0.1 and variable2<=200 then result='False'; else result='True';

alternatively

 

if variable1>0.1 or variable2>200 then result='True'; else result='False';

There is no such thing as an "and/or" here (or anywhere in SAS).

--
Paige Miller

SAS Innovate 2025: Call for Content

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!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 11 replies
  • 1067 views
  • 2 likes
  • 4 in conversation