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

How to use Loop/macro for following code to reduce lines. 

I need to subset dataset if var1 eq 0 AND new_var1 <> 0 

I have to use this condition for 10 times. 

 

data want;
set have;
where (Var1 = 0 AND New_var1<> 0) OR
(Var2 = 0 AND New_var2 <> 0) OR
(Var3 = 0 AND New_var3 <> 0) OR
(Var4 = 0 AND New_var4 <> 0) OR
(Var5 = 0 AND New_var5 <> 0) OR
(Var6 = 0 AND New_var6 <> 0) OR
(Var7 = 0 AND New_var7 <> 0) OR
(Var8 = 0 AND New_var8 <> 0) OR
(Var9 = 0 AND New_var9 <> 0) OR
(Var10 = 0 AND New_var10 <> 0);
run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

Before resorting to code generation consider how you can use the SAS language itself to do what you want.

 

To perform the same action on a series of variables use an ARRAY.  In your case two arrays.

You only need to keep checking until you find one pair that meets the condition.

data want;
  set have;
  array orig var1-var10;
  array new new_var1-new_var10;
  found=0;
  do index=1 to dim(orig) while (not found);
     if orig[index]=0 and new[index] ne 0 then found=1;
  end;
  if found;
run;

 

View solution in original post

4 REPLIES 4
ballardw
Super User

Two sets of variables used like that makes me suspect that perhaps your data is structured incorrectly and you should have data with a Var, New_Var and a Level to hold the 1, 2, etc. Then your code would be :

 

Data want;  
   set have;
   if Var=0 and New_var ne 0;
run;

Note the the symbol <> is not the "not equal" if that is what you are attempting, but is the MAX operator and returns the larger of the values compared. This may accomplish what you want when comparing with 0 but don't rely on it behaving as a "not equal".

 

If you have already written the code why do you need want to make the code harder to understand by introducing macro looping?

Tom
Super User Tom
Super User

Before resorting to code generation consider how you can use the SAS language itself to do what you want.

 

To perform the same action on a series of variables use an ARRAY.  In your case two arrays.

You only need to keep checking until you find one pair that meets the condition.

data want;
  set have;
  array orig var1-var10;
  array new new_var1-new_var10;
  found=0;
  do index=1 to dim(orig) while (not found);
     if orig[index]=0 and new[index] ne 0 then found=1;
  end;
  if found;
run;

 

vignesh-b
Fluorite | Level 6

We can do like

 

if sum(Var1-Var10)=0 and sum(New_var1-New_var10) ne 0 then do;
end;

/* if the above does not work try the below */

if sum(of Var1-Var10)=0 and sum(of New_var1-New_var10) ne 0 then do;
end;
Tom
Super User Tom
Super User

Note that SUM(VAR1-VAR10) is just same as VAR1-VAR10 since you have only give the SUM() function one expression at add up.

 

It is the keyword OF in your second version that makes the hyphen indicate a variable list instead of meaning subtraction.

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
  • 4 replies
  • 825 views
  • 4 likes
  • 4 in conversation