BookmarkSubscribeRSS Feed
yoyong
Obsidian | Level 7

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

Group_C1    Group_C2

where the responses are 1=Yes, 0 = No

 

Treat_A1    Treat_A2       Treat_A3         Treat_A4

Treat_B1    Treat_B2       Treat_B3

Treat_C1    Treat_C2       

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!

 

 

       

 

2 REPLIES 2
art297
Opal | Level 21

It would help if you provided either your dataset, or a subset of your dataset, preferably in the form of a datastep.

 

Art, CEO, AnalystFinder.com

 

ballardw
Super User

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[1], group_a2 is g[2]. 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.

 

SAS Innovate 2025: Register Now

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!

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
  • 2 replies
  • 1084 views
  • 0 likes
  • 3 in conversation