loop or array?

Accepted Solution Solved
Reply
Regular Contributor
Posts: 217
Accepted Solution

loop or array?

Dear all,

Wishing you all well. Can I please ask how do I rewrite my sas code below so I don't have to repeat the if-else statements 32 times? :smileyconfused: Thank you very much Smiley Happy

  q71l_type1=0;

  if q71l_a_q1_1=1 or q71l_a_q1_1=1 or q71l_b_q1_1=1 or q71l_c_q1_1=1 or q71l_aa_q1_1=1 or q71l_d_q1_1=1 or  q71l_e_q1_1=1 or           q71l_f_q1_1=1 or q71l_g_q1_1=1 or q71l_h_q1_1=1 or q71l_i_q1_1=1 or q71l_j_q1_1=1 or q71l_k_q1_1=1 or q71l_cc_q1_1=1 or

    q71l_n_q1_1=1 or q71l_o_q1_1=1 or q71l_dd_q1_1=1 or  q71l_p_q1_1=1 or q71l_q_q1_1=1 or q71l_r_q1_1=1 or q71l_s_q1_1=1 or

    q71l_bb_q1_1=1 then q71l_type1=1;

  if q71l_a_q1_1=. and q71l_a_q1_1=. and q71l_b_q1_1=. and  q71l_c_q1_1=. and q71l_aa_q1_1=. and q71l_d_q1_1=. and q71l_e_q1_1=.     and q71l_f_q1_1=. and q71l_g_q1_1=. and q71l_h_q1_1=. and q71l_i_q1_1=. and q71l_j_q1_1=. and q71l_k_q1_1=. and q71l_cc_q1_1=.     and q71l_n_q1_1=. and q71l_o_q1_1=. and q71l_dd_q1_1=. and  q71l_p_q1_1=. and q71l_q_q1_1=. and q71l_r_q1_1=. and                       q71l_s_q1_1=. and q71l_bb_q1_1=. then q71l_type1=.;

      up to

  q71l_type32=0;

  if q71l_a_q1_32=1 or q71l_a_q1_32=1 or q71l_b_q1_32=1 or q71l_c_q1_32=1 or q71l_aa_q1_32=1 or q71l_d_q1_32=1 or                              q71l_e_q1_32=1 or q71l_f_q1_32=1 or q71l_g_q1_32=1 or q71l_h_q1_32=1 or q71l_i_q1_32=1 or q71l_j_q1_32=1 or q71l_k_q1_32=1 or      q71l_cc_q1_32=1 or  q71l_n_q1_32=1 or q71l_o_q1_32=1 or q71l_dd_q1_32=1 or  q71l_p_q1_32=1 or q71l_q_q1_32=1 or                          q71l_r_q1_32=1 or q71l_s_q1_32=1 or q71l_bb_q1_32=1 then q71l_type32=1;

  if q71l_a_q1_32=. and q71l_a_q1_32=. and q71l_b_q1_32=. and  q71l_c_q1_32=. and q71l_aa_q1_32=. and q71l_d_q1_32=. and                  q71l_e_q1_32=.    and q71l_f_q1_32=. and q71l_g_q1_32=. and q71l_h_q1_32=. and q71l_i_q1_32=. and q71l_j_q1_32=. and                    q71l_k_q1_32=. and q71l_cc_q1_32=.    and q71l_n_q1_32=. and q71l_o_q1_32=. and q71l_dd_q1_32=. and  q71l_p_q1_32=. and              q71l_q_q1_32=. and q71l_r_q1_32=. and  q71l_s_q1_32=. and q71l_bb_q1_32=. then q71l_type32=.;


Accepted Solutions
Solution
‎11-04-2014 04:20 AM
Super User
Super User
Posts: 7,401

Re: loop or array?

One other suggestion, apart from arrays and lots of code, is to normalize your data, i.e.

Question           Response

q71l_a_q1_1     0

q71l_b_q1_1     1

Then its pretty simple to check existence of conditions.  I.e:

proc sql;

     create table WANT as

     select     distinct "1" as RESULT

     from       HAVE

     where     RESPONSE=1;

quit;

View solution in original post


All Replies
Contributor
Posts: 61

Re: loop or array?

Hi,

As I understood, only the last number of the variables are getting chnged for each of the conditions and statements.

In this case you can create a macro variabe which substitutes the number and, you can craete the loop with in the macro or you can loop the macro call 32 times to get this contion checked.

eg:

%macro condi1;

%local xyz;

%do xyz = 1 to 32;

q71l_type&xyz = 0;

if q71l_a_q1_&xyz =1 or ......

%end;

%mend condi1;

you can call the macro condi1 in your peogram where you need to check this condition.

Valued Guide
Posts: 3,208

Re: loop or array?

When this is Boolean logic with a 3 value logic (0 1 en .) or just binary ( 0 1) there are smarter ways to code.

SAS(R) 9.3 Functions and CALL Routines: Reference sum  note the option to use a variable list.

SAS(R) 9.3 Functions and CALL Routines: Reference nmiss cmiss
That is the inner test.

Than you are going to do that 32 times exactly the same way.

A macro is adding complexity to coding in segregating the parts. An array on the definition and looping.

Both are requiring the inner block is equal. I am not seeing that equality on the inner block.

---->-- ja karman --<-----
Super User
Posts: 17,829

Re: loop or array?

I'm not seeing the naming convention for the variables but arrays are usually a better solution.

The logic appears to be if any is 1, set indicator to 1, if all are missing set to missing otherwise set to 0.

If you have a naming convention, you may be able to avoid listing out all the variables manually.

data want;

set have;

array qa(*) list all variables here;

if nmiss(of qa(*)) = dim(qa) then q71l_type1=.

else if max(of qa(*))=1 then q71l_type1=1;

else if max(of qa(*))=1 then q71l_type1=0;


run;

Regular Contributor
Posts: 217

Re: loop or array?

Thanks everyone for your valuable input Smiley Happy

Solution
‎11-04-2014 04:20 AM
Super User
Super User
Posts: 7,401

Re: loop or array?

One other suggestion, apart from arrays and lots of code, is to normalize your data, i.e.

Question           Response

q71l_a_q1_1     0

q71l_b_q1_1     1

Then its pretty simple to check existence of conditions.  I.e:

proc sql;

     create table WANT as

     select     distinct "1" as RESULT

     from       HAVE

     where     RESPONSE=1;

quit;

☑ This topic is SOLVED.

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

Discussion stats
  • 5 replies
  • 318 views
  • 10 likes
  • 5 in conversation