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.
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.
IF sum(QI020=1 , QI021=1 , QI022=1 , QI023=1 , QI024=1 , QI027=1) >= 2 THEN GE1=1;
Shouldn't it be like this with a > sign?
IF sum(QI020, QI021, QI022, QI023, QI024, QI027) >= 2 THEN GE1=1;
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 wrote:
ge 2Is 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 wrote:
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.
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 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: