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

I would like to use the same if statement across different variables. I have 31 of these variables and would like to avoid typing multiple IF statements. Is there a more elegant way to achieve this? Here is my code:

data want;
set have;

%let ab = ab1-ab31;

if a1 = '0' and b1='0' then ab1='1';
else ab1='0';
if a2 = '0' and b2='0' then ab2='1';
else ab2='0';
if a3= '0' and b3='0' then ab3='1';
else ab3='0';

...

if a31 = '0' and b31='0' then ab31='1';
else ab31='0';

n_ab=sum((countc(cats(of &ab),'1')));

drop &ab;
run;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Since you're only interested in the sum of true conditions, this should do it:

data want;
set have;
n_ab = 0;
array a {*} a1-a31;
array b {*} b1-b31;
do i = 1 to 31;
  n_ab = n_ab + (a{i} = "0" and b{i} = "0");
end;
drop i;
run;

View solution in original post

4 REPLIES 4
Kurt_Bremser
Super User

Since you're only interested in the sum of true conditions, this should do it:

data want;
set have;
n_ab = 0;
array a {*} a1-a31;
array b {*} b1-b31;
do i = 1 to 31;
  n_ab = n_ab + (a{i} = "0" and b{i} = "0");
end;
drop i;
run;
ballardw
Super User

This may be a silly question to you but why are you creating so many character variables with values of '1' and '0' instead of numeric variables? Especially considering you are dropping them ?

 

My take assumes you don't already have AB variables so create numeric versions to SUM to get that count.

data want;
   set have;
   array a(*) a1-a31;
   array b(*) b1-b31;
   array ab(*) ab1-ab31;

   do i=1 to dim(a);
      ab[i]=( a[i]='0' and b[i]='0');
   end;
   
   n_ab=sum(of ab(*));
   drop i ab1-ab31 ;
run;

 

 

jmontefalcon
Calcite | Level 5
Hello,

The character variables values each represent tooth conditions (tc1_2 for tooth condition 1 for tooth #2 & tc2_2 for tooth condition 2 for tooth #2).

There are 28 teeth total with tooth condition codes ranging from 0 to 9 and need to obtain sum variables based on different combinations of tooth conditions for each ID.

Not sure if this answers your question, but this is what the code is supposed to be representing.

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 880 views
  • 3 likes
  • 3 in conversation