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
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=.;
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;
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.
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.
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;
Thanks everyone for your valuable input
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;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.