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?
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.
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;
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.
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;
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.
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.
This worked right off the bat. Excellent and elegant answer. Thank you for the help.
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: