Help using Base SAS procedures

How to create a frequency table from multiple variables

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 13
Accepted Solution

How to create a frequency table from multiple variables

Hi,

I am in need of some assistance here. I am trying to create a dataset with frequencies, as mapped over several variables/attributes.

My 'have' set is as follows:

data have;

     input ID a b c d e f END;

cards;

1 X X X Y X Z K

922 X Y Y Z Y X K

33 W Z Y Y X X K

12 X X W W X Y F

;

run;

The value domain of each variable/attribute a .. f is the same.

From this dataset 'have' I wish to create a new set where each variable is the frequency of that value in 'have'.

For instance, if we are to use the above dataset 'have', the resultant 'want' is:

data want;

     input ID X Y Z W END;

cards;

1 4 1 1 0 K

922 2 3 1 0 K

33 2 2 1 1 K

12 3 1 0 2 F

;

run;

Can you give me some suggestions on how to accomplish this feat?


Accepted Solutions
Solution
‎10-23-2014 08:18 AM
Respected Advisor
Posts: 3,777

Re: How to create a frequency table from multiple variables

Your input data with replicates represented in variables only serves to obfuscate the true task.  I would relieve the burden posed by variables a--f and concentrate on their values.

data have; 
  
input ID (a b c d e f END)(:$1.); 
  
cards
1 X X X Y X Z K 
922 X Y Y Z Y X K 
33 W Z Y Y X X K 
12 X X W W X Y F 
;;;; 
  
run
proc transpose data=have out=have2;
   by id notsorted;
  
var a--f;
   run;
proc summary data=have2 nway completetypes order=data;
   class id col1;
   output out=counts;
   run;
proc transpose data=counts out=want(drop=_Smiley Happy;
   by id notsorted;
  
id col1;
   var _freq_;
   run;

10-23-2014 7-17-12 AM.png

View solution in original post


All Replies
Super User
Super User
Posts: 7,392

Re: How to create a frequency table from multiple variables

Hi,

data have;
  infile datalines dlm="," missover;
  input id a $ b $ c $ d $ e $ f $ END $; 
datalines; 
1,X,X,X,Y,X,Z,K
922,X,Y,Y,Z,Y,X,K
33,W,Z,Y,Y,X,X,K
12,X,X,W,W,X,Y,F

run; 

data want (keep=id res1-res4 end);
  set have;
  array cols{6} a b c d e f;
  array res{4} 8. (0,0,0,0);
  array test{4} $20. ('X','Y','Z','W');
  do i=1 to 4;
    do j=1 to 6;
      if cols{j}=test{i} then res{i}=sum(res{i},1);
    end;
  end;
run;

Occasional Contributor
Posts: 13

Re: How to create a frequency table from multiple variables

Thank you very much.

The procedure you listed did unfortunately count not only the individual frequencies, but accumulated them in each subsequent observation. It did however prove very helpful in showing me how it could be done. So thank you.

Super User
Posts: 6,927

Re: How to create a frequency table from multiple variables

data want (keep=ID X Y Z W);

set have;

array field {*} a b c d e f;

X = 0; Y = y; Z = 0; W = 0;

do i = 1 to 6;

  select (field{i});

    when ('X') X = X + 1;

    when ('Y') Y = Y + 1;

    when ('Z') Z = Z + 1;

    when ('W') W = W + 1;

  end;

end;

run;

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor
Posts: 13

Re: How to create a frequency table from multiple variables

This was a great way to do it I think. There was however an issue with counting, as expanding on your solution caused some errors regarding 'conversion from integer to character', though it is probable that this was due to my understanding rather than any irregularity in the procedure.

Thank you for the great answer.

Solution
‎10-23-2014 08:18 AM
Respected Advisor
Posts: 3,777

Re: How to create a frequency table from multiple variables

Your input data with replicates represented in variables only serves to obfuscate the true task.  I would relieve the burden posed by variables a--f and concentrate on their values.

data have; 
  
input ID (a b c d e f END)(:$1.); 
  
cards
1 X X X Y X Z K 
922 X Y Y Z Y X K 
33 W Z Y Y X X K 
12 X X W W X Y F 
;;;; 
  
run
proc transpose data=have out=have2;
   by id notsorted;
  
var a--f;
   run;
proc summary data=have2 nway completetypes order=data;
   class id col1;
   output out=counts;
   run;
proc transpose data=counts out=want(drop=_Smiley Happy;
   by id notsorted;
  
id col1;
   var _freq_;
   run;

10-23-2014 7-17-12 AM.png
Occasional Contributor
Posts: 13

Re: How to create a frequency table from multiple variables

This worked right off the bat. Excellent and elegant answer. Thank you for the help.

☑ This topic is SOLVED.

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

Discussion stats
  • 6 replies
  • 261 views
  • 6 likes
  • 4 in conversation