4 weeks ago - last edited 4 weeks ago
Hi. I need help with proc freq and recoding.
I have two sets of variables:
Group_A1 Group_A2 Group_A3 Group_A4
Group_B1 Group_B2 Group_B3
where the responses are 1=Yes, 0 = No
Treat_A1 Treat_A2 Treat_A3 Treat_A4
Treat_B1 Treat_B2 Treat_B3
where the responses are 1 = Agree 2 = Neutral 3 = Disagree
a. First, I need to do a PROC FREQ for each pair of variables (Group_A1 and Treat_A1, Group_A2 and Treat_A2, Group_A3 and Treat_A3....., Group_C2 and Treat_C2).
b. Then, using each pair of variables, I need to recode where the rule is: if Group = Yes and Treat = Agree, = 1, else = 0. My new variables would have to be A1, A2, A3,...C2.
How do I code it without doing proc freq and recoding multiple times?
Thanks for your help!
4 weeks ago
Your recoding is unrelated to Proc Freq as far as I can tell and is pretty easy using arrays.
data want; set have; array g group_a1- group_a4 group_b1- group_b3 group_c1- group_c2; array t treat_a1- treat_a4 treat_b1- treat_b3 treat_c1- treat_c2; array f a1- a4 b1- b3 c1- c2; do i= 1 to dim(g); f[i] = (g[i]=1 and t[i]=1); end; drop i; run;
array is way to reference variables using an index number. The variables are added to the array definition above using one of the List methods: group_a1-group_a4 assigns all the variables in the range indicated in order for example.
The short hand to use group_a1 is g, group_a2 is g. The variable in the "do I=" is used as the index.
The proc freq might be done with something like this:
proc freq data= have; tables Group_A1*Treat_A1 Group_A2*Treat_A2 Group_A3*Treat_A3 Group_A4*Treat_A4 ; run;
and use the editor to copy the A parts and paste below and edit to have the needed B or C.
However for another, and often better is to not have data in the variable names. The following restructures the data so that each records is from A1, A2.. and then uses the classification variable I call class to use BY group processing to do the many freq requests.
data want; set have; array g group_a1- group_a4 group_b1- group_b3 group_c1- group_c2; array t treat_a1- treat_a4 treat_b1- treat_b3 treat_c1- treat_c2; do i= 1 to dim(g); class= scan(vname(g[i]),2,'_'); group= g[i]; treat= t[i]; val = (g[i]=1 and t[i]=1); end; keep class group treat val; run; proc sort data=want; by class; run; proc freq data=want; by class; tables group*treat; run;
This is one way of transforming data. Proc transpose can do similar for other data structures.