DATA Step, Macro, Functions and more

IF statement logic

Accepted Solution Solved
Reply
Contributor
Posts: 71
Accepted Solution

IF statement logic

[ Edited ]

Hi,

Have 

var1 var2 var3 var4

1       2      2      4

1       3      3      3

1       2      2      0

1      5       5      5

 

 

want if var1 =1 and value of var2 , var3 or var4 is either 2 or 3 then flag =1 else 0. 

 

want

var1 var2 var3 var4 flag

1       2      2      4      1

1       3      3      3       1

1       2      2      0        1

1      5       5      5        0

 

thanks

 


Accepted Solutions
Solution
‎07-12-2016 08:07 PM
Super User
Posts: 19,789

Re: iF STATEMENT

If var1=1 and (var2 in (2 3) or var3 in (2 3) or var4 in (2 3)) then flag=1;
Else flag=0;

 

View solution in original post


All Replies
Solution
‎07-12-2016 08:07 PM
Super User
Posts: 19,789

Re: iF STATEMENT

If var1=1 and (var2 in (2 3) or var3 in (2 3) or var4 in (2 3)) then flag=1;
Else flag=0;

 

Contributor
Posts: 71

Re: iF STATEMENT

[ Edited ]

Thank you am also looking for an easy if statement 

have

var1  var2  var3  var4  flag (want)

a        b        b      a        0

a       a                 a        1

a                                    1

 

if the value (could be any) but in above eg its "a" of var1 matches with var1 2 or 3 then flag 1 else 0

All vlaues should match var1 or be equal to var1 

 

Thanks

Super User
Posts: 19,789

Re: iF STATEMENT

You should start a new thread for a new question. 

 

'Easiest' depends on your programming skills. 

 

For something like this your best of looping through all values.

 

array vars(3) var2-var4;

Flag=0;

do i =1 to dim(vars);

if  vars(I) ne var1 then flag=1;

end;

 

 

Another option 

 

array vars(3) var2-var4;

flag=0;

if catt(of vars(*)) ne repeat(var1, n(of vars(*))) then flag=1;

 

Super User
Posts: 11,343

Re: IF statement logic

Here is an alternate approach to consider if you have more variables to search but still have a smallish number of values.

 

array t var2-var4;
flag = (var1=1 and (whichn(2, of t(*))>0 or whichn(3, of t(*))>0));

if you have 15 variables to search in for example then you just add them to the Array definition.

 

If you want to search for character values use WhichC instead of WhichN.

☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 392 views
  • 2 likes
  • 3 in conversation