Hello,
I have a dataset filled with character variables.
data have;
| patient | infection_1 | infection_2 | infection_3 |
| abc | virus | ||
| xyz | fungus | virus | |
| lmn | bacteria | fungus | virus |
I want to produce one frequency table to find all occurrences in the variable:
| any infection | |
| virus | 3 |
| fungus | 2 |
| bacteria | 1 |
Is there a shorter way in SAS to do this? Thank you!
data have;
input (patient infection_1 infection_2 infection_3) (:$10.);
cards;
abc virus . .
xyz fungus virus .
lmn bacteria fungus virus
;
proc transpose data=have out=_have(keep=col1 where=(col1 ne ' ' ));
by patient notsorted;
var infection:;
run;
proc freq data=_have noprint;
tables col1/out=want(keep=col1 count);
run;
data need (keep=infection) / view=need;
set have ;
array inf {*} infection_: ;
length infection $12;
do I=1 to dim(inf) while (inf{I}^=' ');
infection=inf{I};
output;
end;
run;
proc freq data=need;
tables infection;
run;
This program assumes that if a row has fewer than 3 infections, the non-blank values are the leftmost.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.
Ready to level-up your skills? Choose your own adventure.