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

SAS novice here! Using version 9.4

I would like to be able to combine the rows below by Spec. Ideally I want to keep anything with Agent='Klesbiella pneumoniae' (there are several other bacterial names that I would want to keep) and also keep the results for CIM, KPC, and NDM that are associated with the other agent names. Here is an example of the data. 

Proj Spec Coldt Rcvddt Lname Fname Agent Quant CIM KPC NDM
2176 11367 11-Jul-2017 19-Jul-2017 Burger Ham CARBAPENEMASE DETECTED 1 . .
2176 11367 11-Jul-2017 19-Jul-2017 Burger Ham KLEBSIELLA PNEUMONIAE NULL . . .
2176 11367 11-Jul-2017 19-Jul-2017 Burger Ham KPC DNA PRESENT . 1 .
2176 11367 11-Jul-2017 19-Jul-2017 Burger Ham NDM DNA NOT DETECTED . . 0

 

I was able to combine rows using the following code:

data arln;
set all;
by spec;
if first.spec then n=0;
n+1;
run;
proc sql noprint;
select distinct catt ('arln (where=(n=',left(put(n,8.)),
		') rename=(agent=agent',left(put(n,8.)),
		' quant=quant',left(put(n,8.)),
		'))')
	into :mer separated by ' '
	from arln;
	quit;
data arln;
merge &mer;
by spec;
drop n;
run;

 

This worked like a charm except that many of the multiple rows are in a different order than what I've shown above (e.g., KPC DNA might be the first row of the bunch). So what I've now got are multiple agents and multiple quants and many of the results for CIM, KPC, and NDM are gone. I managed to get to the results I wanted by writing a five page if-then statement since each agent now could be anywhere from Agent1 - Agent5 for each row:

data arln1;
set arln;	
if Agent1='CARBAPENEMASE' and Quant1='DETECTED' then CIM=1;
else if Agent1='CARBAPENEMASE' and Quant1='POSITIVE' then CIM=1;
else if Agent1='CARBAPENEMASE' and Quant1='NOT DETECTED' then CIM=0;
else if Agent1='CARBAPENEMASE' and Quant1='NEGATIVE' then CIM=0;
else if Agent1='CARBAPENEMASE' and Quant1='INCONCLUSIVE' then CIM=2;
else if Agent1='CARBAPENEMASE' and Quant1='INDETERMINATE' then CIM=2;
else if Agent1='CARBAPENEMASE' and Quant1='NOT TESTED' then CIM=9;
else if Agent2='CARBAPENEMASE' and Quant2='DETECTED' then CIM=1;
else if Agent2='CARBAPENEMASE' and Quant2='POSITIVE' then CIM=1;
else if Agent2='CARBAPENEMASE' and Quant2='NOT DETECTED' then CIM=0;
else if Agent2='CARBAPENEMASE' and Quant2='NEGATIVE' then CIM=0;
else if Agent2='CARBAPENEMASE' and Quant2='INCONCLUSIVE' then CIM=2;
else if Agent2='CARBAPENEMASE' and Quant2='INDETERMINATE' then CIM=2;
else if Agent2='CARBAPENEMASE' and Quant2='NOT TESTED' then CIM=9;

I feel like there has got to be a better way to do what needs to be done. 

 

Here's what I'm looking for, although I wouldn't care one way or the other if the other agent names remained as Agent1-Agentn:

Proj Spec Coldt Rcvddt Lname Fname Agent Quant CIM KPC NDM
2176 11367 11-Jul-2017 19-Jul-2017 Burger Ham KLEBSIELLA PNEUMONIAE NULL 1 1 0

 

Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

Looks like you want something like this:

 

data have;
infile datalines dsd;
informat Proj Spec best. Coldt Rcvddt date. lname fname agent quant $24.;
format Coldt Rcvddt yymmdd10.;
input Proj Spec Coldt Rcvddt Lname Fname Agent Quant CIM KPC NDM;
datalines;
2176,11367,11-Jul-2017,19-Jul-2017,Burger,Ham,CARBAPENEMASE,DETECTED,1,.,.,
2176,11367,11-Jul-2017,19-Jul-2017,Burger,Ham,KLEBSIELLA PNEUMONIAE,NULL,.,.,.,
2176,11367,11-Jul-2017,19-Jul-2017,Burger,Ham,KPC DNA,PRESENT,.,1,.,
2176,11367,11-Jul-2017,19-Jul-2017,Burger,Ham,NDM DNA,NOT DETECTED,.,.,0
;

data want;
do until(last.Fname);
    set have; by Proj Spec Coldt Rcvddt Lname Fname notsorted;
    _CIM = coalesce(_CIM, CIM);
    _KPC = coalesce(_KPC, KPC);
    _NDM = coalesce(_NDM, NDM);
    end;

do until(last.Fname);
    set have; by Proj Spec Coldt Rcvddt Lname Fname notsorted;
    if Agent in ("KLEBSIELLA PNEUMONIAE" /* Complete list */) 
        then output;
    end;
drop CIM KPC NDM;
rename _CIM=CIM _KPC=KPC _NDM=NDM;
run;
PG

View solution in original post

2 REPLIES 2
PGStats
Opal | Level 21

Looks like you want something like this:

 

data have;
infile datalines dsd;
informat Proj Spec best. Coldt Rcvddt date. lname fname agent quant $24.;
format Coldt Rcvddt yymmdd10.;
input Proj Spec Coldt Rcvddt Lname Fname Agent Quant CIM KPC NDM;
datalines;
2176,11367,11-Jul-2017,19-Jul-2017,Burger,Ham,CARBAPENEMASE,DETECTED,1,.,.,
2176,11367,11-Jul-2017,19-Jul-2017,Burger,Ham,KLEBSIELLA PNEUMONIAE,NULL,.,.,.,
2176,11367,11-Jul-2017,19-Jul-2017,Burger,Ham,KPC DNA,PRESENT,.,1,.,
2176,11367,11-Jul-2017,19-Jul-2017,Burger,Ham,NDM DNA,NOT DETECTED,.,.,0
;

data want;
do until(last.Fname);
    set have; by Proj Spec Coldt Rcvddt Lname Fname notsorted;
    _CIM = coalesce(_CIM, CIM);
    _KPC = coalesce(_KPC, KPC);
    _NDM = coalesce(_NDM, NDM);
    end;

do until(last.Fname);
    set have; by Proj Spec Coldt Rcvddt Lname Fname notsorted;
    if Agent in ("KLEBSIELLA PNEUMONIAE" /* Complete list */) 
        then output;
    end;
drop CIM KPC NDM;
rename _CIM=CIM _KPC=KPC _NDM=NDM;
run;
PG
gros0265
Calcite | Level 5

Thank you PG!

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 1294 views
  • 0 likes
  • 2 in conversation