Help using Base SAS procedures

How to combine multiple rows without losing data

Accepted Solution Solved
Reply
New Contributor
Posts: 4
Accepted Solution

How to combine multiple rows without losing data

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!


Accepted Solutions
Solution
‎10-24-2017 05:03 PM
Esteemed Advisor
Posts: 5,526

Re: How to combine multiple rows without losing data

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


All Replies
Solution
‎10-24-2017 05:03 PM
Esteemed Advisor
Posts: 5,526

Re: How to combine multiple rows without losing data

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
New Contributor
Posts: 4

Re: How to combine multiple rows without losing data

Thank you PG!

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 236 views
  • 0 likes
  • 2 in conversation