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

I want to define a variable which it assumes a value if at least two of six variables assume a value 1.

 

If it were only one variable the code should be this one:

 

FORMAT GE1 $1.;
IF QI020=1 OR QI021=1 OR QI022=1 OR QI023=1 OR QI024=1 OR QI027=1 THEN GE1=1;

Which code should I write for as least two options validated?

 

Thank you for your help.

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

If the other values for those variables are 0 or missing use something like:

 

IF sum( QI020,QI021,QI022,QI023,QI024,QI027) ge 2 THEN GE1=1;

If you want Ge1 to be character use ='1';

 

 

If the other values can take more of a range then

IF sum( QI020=1, QI021=1, QI022=1, QI023=1, QI024=1, QI027=1) ge 2 THEN GE1=1;

SAS will use a value of 1 for a true comparison and 0 otherwise. So the Sum above gets the number of "true" comparison results.

 

View solution in original post

8 REPLIES 8
Ksharp
Super User

IF sum(QI020=1 , QI021=1 , QI022=1 , QI023=1 , QI024=1 , QI027=1)  >= 2 THEN GE1=1;

ramgouveia
Obsidian | Level 7

@Ksharp 

 

Shouldn't it be like this with a > sign?

IF sum(QI020, QI021, QI022, QI023, QI024, QI027) >= 2 THEN GE1=1;

 

Ksharp
Super User
Yes. You are right. Just correct it .
ballardw
Super User

If the other values for those variables are 0 or missing use something like:

 

IF sum( QI020,QI021,QI022,QI023,QI024,QI027) ge 2 THEN GE1=1;

If you want Ge1 to be character use ='1';

 

 

If the other values can take more of a range then

IF sum( QI020=1, QI021=1, QI022=1, QI023=1, QI024=1, QI027=1) ge 2 THEN GE1=1;

SAS will use a value of 1 for a true comparison and 0 otherwise. So the Sum above gets the number of "true" comparison results.

 

ramgouveia
Obsidian | Level 7

@ballardw 

 

ge 2

Is the same to say equal or greater than 2? 

ballardw
Super User

@ramgouveia wrote:

@ballardw 

 

ge 2

Is the same to say equal or greater than 2? 


Yes. I tend to use the ge and le instead of >= or<=  because sometimes if you get the order wrong (typo) they can mean different things in some code

ramgouveia
Obsidian | Level 7

@ballardw 

 

And if it were only "equal to"? The symbol "=" would be good enough?

ballardw
Super User

@ramgouveia wrote:

@ballardw 

 

And if it were only "equal to"? The symbol "=" would be good enough?


Yes.

Or EQ  , GT (greater than: > ) , LT (less than: <)

 

Sometimes in specific code it may be helpful to be able to tell the difference between an "assignment" = sign or a "comparison" equal sign. Though I suspect part of the text comes from 1) ancient keyboards with fewer characters and 2) derivation from another language like FORTRAN when coding syntax decisions were made.

 

It might be appropriate to note that this method of counting also works with Character values or a mix because the comparisons would also return 1/0:

 

if sum(var1='A', var2='B', var3 in ('P' 'D' 'Q'), var5>7) ge 3

for example.

If you haven't run into the IN comparison you can provide a list of values (NOT variables) to compare and if the variable takes any of the values it returns true. The comparison would be the equivalent of : var3='P' or Var3='D' or Var3='Q'.

 

Use the MIN function instead of SUM as an alternative way of "at least one is true" : min(<comparison list) =1

The Range function can tell if they all have the same result (all true or all false and Range will return 0)

 

Caution: Missing is always "less than" any value so may need to consider that when using < or <= as comparisons.

 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 1596 views
  • 6 likes
  • 3 in conversation