BookmarkSubscribeRSS Feed
jcapua2
Fluorite | Level 6

Hello,

 

I have a dataset filled with character variables.

 

data have;

patientinfection_1infection_2infection_3
abcvirus  
xyzfungusvirus 
lmnbacteriafungusvirus

 

I want to produce one frequency table to find all occurrences in the variable:

 any infection
virus3
fungus2
bacteria1

 

Is there a shorter way in SAS to do this? Thank you!

2 REPLIES 2
novinosrin
Tourmaline | Level 20
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;
mkeintz
PROC Star
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.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 2 replies
  • 1394 views
  • 1 like
  • 3 in conversation