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

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?

1 ACCEPTED SOLUTION

Accepted Solutions
data_null__
Jade | Level 19

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=_:);
   by id notsorted;
  
id col1;
   var _freq_;
   run;

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

View solution in original post

6 REPLIES 6
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;

Dess
Calcite | Level 5

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.

Kurt_Bremser
Super User

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;

Dess
Calcite | Level 5

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.

data_null__
Jade | Level 19

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=_:);
   by id notsorted;
  
id col1;
   var _freq_;
   run;

10-23-2014 7-17-12 AM.png
Dess
Calcite | Level 5

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 6 replies
  • 1104 views
  • 6 likes
  • 4 in conversation