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

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=.;

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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

5 REPLIES 5
DMoovendhan
Quartz | Level 8

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.

jakarman
Barite | Level 11

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

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;

Miracle
Barite | Level 11

Thanks everyone for your valuable input Smiley Happy

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

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
  • 5 replies
  • 732 views
  • 10 likes
  • 5 in conversation