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

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User
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

4 REPLIES 4
Reeza
Super User
If var1=1 and (var2 in (2 3) or var3 in (2 3) or var4 in (2 3)) then flag=1;
Else flag=0;

 

AZIQ1
Quartz | Level 8

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

Reeza
Super User

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;

 

ballardw
Super User

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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 4 replies
  • 1092 views
  • 2 likes
  • 3 in conversation