Help using Base SAS procedures

Flag items with same unique ID but different categories for a variable

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 11
Accepted Solution

Flag items with same unique ID but different categories for a variable

[ Edited ]

Hello,

 

data have;

patient_numProcedure DateStatus DateOrgan
patient1103/07/200202/23/2004Heart
patient1102/23/200410/01/2014Lung
patient1204/23/200610/13/2014Heart
patient1310/21/200710/10/2016Bone
patient1405/06/200904/30/2017Liver
patient1405/09/201007/15/2017Liver

 

data want;

patient_numProcedure DateStatus DateOrganMultiorgan
patient1103/07/200202/23/2004HeartYes
patient1102/23/200410/01/2014LungYes
patient1204/23/200610/13/2014HeartNo
patient1310/21/200710/10/2016BoneNo
patient1405/06/200904/30/2017LiverNo
patient1405/09/201007/15/2017LiverNo

 

I want to flag rows where a patient has multiple organ procedures. So for patient 11, he/she has both heart and lung surgeries. So new variable Multiorgan='Yes'.

 

Is there a way to do this in data step? Or proc SQL?

 

Thank you!


Accepted Solutions
Solution
2 weeks ago
Super User
Posts: 20,716

Re: Flag items with same unique ID but different categories for a variable

SQL is quicker in this case.

 

proc sql;
create table want as
select *, 
      case when max(organ)=min(organ) then 'No'
                   else 'Yes' end 
      as multiorgan
from have
group by patient_num;
quit;

View solution in original post


All Replies
Solution
2 weeks ago
Super User
Posts: 20,716

Re: Flag items with same unique ID but different categories for a variable

SQL is quicker in this case.

 

proc sql;
create table want as
select *, 
      case when max(organ)=min(organ) then 'No'
                   else 'Yes' end 
      as multiorgan
from have
group by patient_num;
quit;
Highlighted
Trusted Advisor
Posts: 1,121

Re: Flag items with same unique ID but different categories for a variable

 

 If the data are sorted by patient_num:

 

data want;

  set have;

  by patient_num;

  if first.patient_num and last.patient_num then multi_organ='No';

  else multi_organ='Yes';

run;

☑ This topic is solved.

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

Discussion stats
  • 2 replies
  • 113 views
  • 2 likes
  • 3 in conversation