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

 

I have a group of variables , say b1-b30 and i need to test same condition on same variables among themselves like, if b1='a' or b2='a' or b3='a'.. and so on. is there any solution in SAS macros or using of sas arrays to do this task to avoid writing this explicitly. sample code is like this:

data test;
    input d1 d2 d3 d4 d5 ;
cards;
1 2 1 1 0
2 3 1 0 0 
0 0 0 1 0
0 2 1 0 2
0 4 0 2 2
0 0 0 0 3
;
run;

data want;
    set test;
    if d1=1 or d2=1 or d3=1 or d4=1 then flag=1;
    else flag=0;
run;

so i have around 50 variable , to test same condition and flag it .

1 ACCEPTED SOLUTION

Accepted Solutions
stat_sas
Ammonite | Level 13

Yes, this is doing OR comparison. This says if any of the d variable equals to 1 then flag will be assigned a value of 1 and 0 otherwise.

View solution in original post

9 REPLIES 9
stat_sas
Ammonite | Level 13

data want(drop=i);

set test;
flag=0;
array d(*) d:;
do i=1 to dim(d);
if d(i)=1 then flag=1;
end;
run;

ved_sas2014
Fluorite | Level 6

Hi , thanks for quick reply, is this doing OR comparison among the variables? kindly explain and also in case we want to use "AND" operator, then how will it work

stat_sas
Ammonite | Level 13

Yes, this is doing OR comparison. This says if any of the d variable equals to 1 then flag will be assigned a value of 1 and 0 otherwise.

ballardw
Super User

The code provided by @stat_sas assigns a default value of 0 to the flag (the Else assignment in the original post). Then it tests each variable in the array for the test value. If any of them match then the flag is set to 1.

So it will provide the equivalent of  var1=1 or var2=1 or ..;

 

ved_sas2014
Fluorite | Level 6

thanks @ballarw . just curious to know, if in case we have "and " operator

FreelanceReinh
Jade | Level 19

Hi @ved_sas2014,

 

If I may step in here: Sure, a slight modification of @stat_sas's code could be used to set FLAG=1 if (and only if) the condition di=1 is true for all i=1, ..., 5 (edit: and FLAG=0 otherwise):

 

data want2(drop=i);
set test;
flag=1;
array d(*) d:;
do i=1 to dim(d);
  if d(i)~=1 then flag=0;
end;
run;

This makes use of the fact that

(d1=1 and d2=1 and ... d5=1)

is logically equivalent to

not (d1~=1 or d2~=1 or ... d5~=1)

 

 

FreelanceReinh
Jade | Level 19

Here is an alternative solution to your original question: You can apply the IN operator to an array.

data want;
set test;
array d d:;  /* Your IF condition rather suggests: array d d1-d4; */
flag = 1 in d;
run;

The condition "1 in d" (meaning that 1 is contained in the set {d(1), d(2), ...}, where the last element in the set depends on the array definition, see above) evaluates to 1 (true) or 0 (false).

 

Reeza
Super User

Look at the WHICHN function. It doesn't return a 1/0 but the index of where the 1 is found. 

For character variables there's a WHICHC function.

 

found=whichn(1, of d1-d100);
FreelanceReinh
Jade | Level 19

@Reeza wrote:

Look at the WHICHN function. It doesn't return a 1/0 but the index of where the 1 is found. 

For character variables there's a WHICHC function.

 

found=whichn(1, of d1-d100);

... and if you need a 1/0 flag, you can simply write:

flag = whichn(1, of d1-d100)>0;

(or, still shorter: ... of d: ... if the variables whose names start with "d" are just d1, ..., d100).

So, you can even save the ARRAY statement.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 9 replies
  • 2179 views
  • 1 like
  • 5 in conversation