Listing multiple diseases for a single patient

MimiM

New Member
Joined
Apr 23, 2018
Messages
24
Hi

I have an SQL extract table of patients with one or more diseases recorded for them. Each patient/disease information is a separate record/line. Through a pivot, I can see which patient has more than one disease recorded and which diseases are included. However, the pivot contains all possible diseases (columns) and all patients (rows).

My aim is to list each individual patient, with only the diseases recorded against them as columns. I would then try to summarise the combinations of diseases.

Any ideas, please?

Many thanks in advance

Mimi

Structure;
Patient 1 Disease A Practice X
Patient 1 Disease F Practice X
Patient 2 Disease B Practice Y
Patient 2 Disease G Practice Y
Patient 2 Disease H Practice Y
Patient 3 Disease A Practice Z
Patient 3 Disease C Practice Z

Aiming at

Patient 1 Practice X Disease A Disease F
Patient 2 Practice Y Disease B Disease G Disease H
Patient 3 Practice Z Disease A Disease C

etc.
 
Thank you, Peter, I believe this should do it. Unfortunately, my VBA knowledge is nil. In order to make the function work, where do I need to change the code to reflect the following:

There are more than 3 columns in the raw data, but all I am interested in is the count of disease combinations - overall, not for each patient. I guess I can cut the data to two columns: patient and disease.

I have counted up to 13 diseases per patient

And - the silliest question - how do I get to write the macro? Just right-click on the sheet - view code - and then type/copy?

Thank you again, and I can only hope that, when I give the person some of this information, she won't come back for even more detail!

Mimi
 
Upvote 0

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
There are more than 3 columns in the raw data,
1. But for this exercise are you only interested in 3 columns: Patient, Disease, & Practice?
- If so, which columns are they?
- If not, can you explain more about which columns are relevant and how/why?

2. Does the sheet have a fixed number of columns, or can it vary?


.. all I am interested in is the count of disease combinations - overall, not for each patient.
3. My code already gives the count of disease combinations overall, not per patient. For example, in my sample data the combination of Measles & Mumps occurred 3 times throughout the data altogether. Isn't that what you are wanting?

4. Do you still actually want the Practice & Diseases for each patient listed across a row like columns E:I in my sample or are you only interested in the combinations count like columns K:L?


And - the silliest question - how do I get to write the macro? Just right-click on the sheet - view code - and then type/copy?
It isn't a silly question - we all had to work that out the first time we came across macros, and you are close. :)
For this type of macro, after you right click and 'View Code', best to then use the menu in the vba window to Insert -> Module and paste the code in the right hand window that is created by that.
Then, to run the macro there are several ways but one is Alt+F8 to bring up the macro dialog, Select the 'Analyse_Diseases' macro & click 'Run'.
The workbook will need to be saved as a macro-enabled workbook (*.xlsm)
 
Upvote 0
1. But for this exercise are you only interested in 3 columns: Patient, Disease, & Practice?
- If so, which columns are they?
- If not, can you explain more about which columns are relevant and how/why?

2. Does the sheet have a fixed number of columns, or can it vary?


3. My code already gives the count of disease combinations overall, not per patient. For example, in my sample data the combination of Measles & Mumps occurred 3 times throughout the data altogether. Isn't that what you are wanting?

4. Do you still actually want the Practice & Diseases for each patient listed across a row like columns E:I in my sample or are you only interested in the combinations count like columns K:L?


It isn't a silly question - we all had to work that out the first time we came across macros, and you are close. :)
For this type of macro, after you right click and 'View Code', best to then use the menu in the vba window to Insert -> Module and paste the code in the right hand window that is created by that.
Then, to run the macro there are several ways but one is Alt+F8 to bring up the macro dialog, Select the 'Analyse_Diseases' macro & click 'Run'.
The workbook will need to be saved as a macro-enabled workbook (*.xlsm)

Thank you again, Peter. Now, in order of questions

1. The columns I am interested in are Patient and Disease
2. I can limit the columns; the outputs have about 6, for other purposes
3. Your code is exactly what I am interested in: I was simply confirming that I am not necessarily interested in looking into each patient record
4. The overall question is about the total count; the listing per patient was my initial attempt at putting some order in the sea of data

And thank you for giving me the steps for the macro.

From reading the code, I am roughly guessing what it is doing and where I might amend, but wouldn't want to break anything. Sometimes Excel can be a bit temperamental, and it's an office computer/server.

Mimi
 
Upvote 0
1. The columns I am interested in are Patient and Disease
Which columns are they? (eg Patient is col A and Disease is Col E)
Or can the columns vary and the code needs to look for the headings? If so, please give exact headings.


2. I can limit the columns; the outputs have about 6, for other purposes
There should be no need to do that. Results can just go off further to the right (the code can work that out) or the results could go on a new worksheet if you would prefer.


3. Your code is exactly what I am interested in: I was simply confirming that I am not necessarily interested in looking into each patient record
4. The overall question is about the total count; the listing per patient was my initial attempt at putting some order in the sea of data
OK, noted. When we resolve the above issues, we will just focus on the sort of results I had previously in columns K:L
 
Upvote 0
I'll need to go to that particular office and see what headings I'm pulling out of the database - this is tomorrow evening, Peter. As soon as I've got them, I'll let you know. Best to go by headings as I may decide to drop or add columns later on.

Funnily enough, in this workplace, a question has risen that may be very similar to the discussion above. Only here, it is about different types of activities, e.g., programs, counselling, online chats - again, the question is - how many people engage in what type of activities, and it seems the scope/combination is the focus of the question. I'll know more later. Might be able to adapt what you've suggested above - eventually.

Will come back tomorrow with the headings.
 
Upvote 0
I'll need to go to that particular office and see what headings I'm pulling out of the database - this is tomorrow evening, Peter. As soon as I've got them, I'll let you know. Best to go by headings as I may decide to drop or add columns later on.
OK, you will just need to ensure that the headings in the code below match the headings in the worksheet. It doesn't matter what columns they are in or what order they are in. I have assumed that they will be in row 1 though.

I have also assumed that if there are multiple sheets in the workbook, the one with this data is the active one when the code is run.

This revised code also adds a new worksheet and puts the summary count on that new worksheet.
Edit the red code as required.
Rich (BB code):
Sub Analyse_Diseases_v2()
  Dim d1 As Object, d2 As Object, AL As Object, SL As Object
  Dim a As Variant, ky As Variant, itm As Variant, aRws As Variant
  Dim i As Long, nr As Long, k As Long, LastVal As Long, lr As Long, ColPatient As Long, ColDisease As Long
  
  Const TopNum As Long = 3  '<- Top how many combinations you want to list
  
  With Rows(1)
    ColPatient = .Find(What:="Patient", LookAt:=xlWhole, MatchCase:=False).Column '<- Column heading to suit your data
    ColDisease = .Find(What:="Disease", LookAt:=xlWhole, MatchCase:=False).Column '<- Column heading to suit your data
  End With
  Set d1 = CreateObject("Scripting.Dictionary")
  d1.CompareMode = 1
  Set d2 = CreateObject("Scripting.Dictionary")
  d2.CompareMode = 1
  Set AL = CreateObject("System.Collections.ArrayList")
  Set SL = CreateObject("System.Collections.Sortedlist")
  lr = Cells(Rows.Count, ColPatient).End(xlUp).Row
  aRws = Evaluate("row(2:" & lr & ")")
  a = Application.Index(Cells, aRws, Array(ColPatient, ColDisease))
  For i = 1 To UBound(a)
    d1(a(i, 1)) = d1(a(i, 1)) & ";" & a(i, 2)
  Next i
  For Each ky In d1.keys()
    AL.Clear
    For Each itm In Split(Mid(d1(ky), 2), ";")
      AL.Add itm
    Next itm
    AL.Sort
    d1(ky) = Join(AL.ToArray, ";")
  Next ky
  For Each itm In d1.Items()
    d2(itm) = d2(itm) + 1
  Next itm
  For Each itm In d2.keys()
    SL.Add Format(d2(itm), "00000") & "|" & itm, 1
  Next itm
  SL.Add "00000|x", 1
  Sheets.Add After:=ActiveSheet
  Range("A1").Value = "Top " & TopNum
  nr = 2
  i = SL.Count
  Do While SL.GetKey(i - 1) <> "00000|x" And (k < TopNum Or Split(SL.GetKey(i - 1), "|")(0) - LastVal = 0)
    nr = nr + 1
    Cells(nr, 1).Resize(, 2).Value = Split(SL.GetKey(i - 1), "|")
    LastVal = Cells(nr, 1).Value
    k = k + 1
    i = i - 1
  Loop
  With Cells(2, 1).Resize(, 2)
    .Value = Array("Count", "Disease Combination")
    .CurrentRegion.Columns(1).TextToColumns DataType:=xlDelimited, Other:=False, FieldInfo:=Array(1, 1)
    .EntireColumn.AutoFit
  End With
End Sub

My new sample data:


Book1
ABCDE
1PatientOther 1Other 2DiseasePractice
2Patient 1datadataAnginaPractice X
3Patient 1datadataAsthmaPractice X
4Patient 2datadataBrain CancerPractice Y
5Patient 2datadataInfluenzaPractice Y
6Patient 2datadataAnginaPractice Y
7Patient 3datadataMumpsPractice Z
8Patient 3datadataMeaslesPractice Z
9Patient 4datadataAsthmaPractice X
10Patient 4datadataHypertensionPractice X
11Patient 5datadataAsthmaPractice X
12Patient 5datadataAnginaPractice X
13Patient 6datadataHypertensionPractice X
14Patient 6datadataAsthmaPractice X
15Patient 7datadataMeaslesPractice X
16Patient 7datadataMumpsPractice X
17Patient 8datadataInfluenzaPractice X
18Patient 8datadataAnginaPractice X
19Patient 8datadataBrain CancerPractice X
20Patient 9datadataMeaslesPractice X
21Patient 9datadataMumpsPractice X
22Patient 10datadataBrain CancerPractice X
23Patient 10datadataInfluenzaPractice X
24Patient 10datadataAnginaPractice X
Data



Result after new code:


Book1
AB
1Top 3
2CountDisease Combination
33Measles;Mumps
43Angina;Brain Cancer;Influenza
52Asthma;Hypertension
62Angina;Asthma
7
Sheet2
 
Upvote 0
This is wonderful, don't know how to thank you, Peter. Will try it tomorrow and keep for future reference

Mimi
 
Upvote 0
This worked a treat - and was ever so fast!

[TABLE="width: 301"]
<colgroup><col><col></colgroup><tbody>[TR]
[TD]Top 5[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Count[/TD]
[TD] Disease Combination[/TD]
[/TR]
[TR]
[TD="align: right"]1677[/TD]
[TD] Diabetes Type 2;Hypertension[/TD]
[/TR]
[TR]
[TD="align: right"]559 [/TD]
[TD] Anxiety Exists;Depression Active[/TD]
[/TR]
[TR]
[TD="align: right"]438[/TD]
[TD] Hypertension;Osteoarthritis[/TD]
[/TR]
[TR]
[TD="align: right"]322[/TD]
[TD] Diabetes Type2;Hypertension;Osteoarthritis[/TD]
[/TR]
[TR]
[TD="align: right"]278[/TD]
[TD] Hypertension;Musculoskeletal Other[/TD]
[/TR]
</tbody>[/TABLE]

Ever so grateful!
 
Upvote 0
This worked a treat - and was ever so fast!

[TABLE="width: 301"]
<colgroup><col><col></colgroup><tbody>[TR]
[TD]Top 5[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Count[/TD]
[TD] Disease Combination[/TD]
[/TR]
[TR]
[TD="align: right"]1677[/TD]
[TD] Diabetes Type 2;Hypertension[/TD]
[/TR]
[TR]
[TD="align: right"]559 [/TD]
[TD] Anxiety Exists;Depression Active[/TD]
[/TR]
[TR]
[TD="align: right"]438[/TD]
[TD] Hypertension;Osteoarthritis[/TD]
[/TR]
[TR]
[TD="align: right"]322[/TD]
[TD] Diabetes Type2;Hypertension;Osteoarthritis[/TD]
[/TR]
[TR]
[TD="align: right"]278[/TD]
[TD] Hypertension;Musculoskeletal Other[/TD]
[/TR]
</tbody>[/TABLE]

Ever so grateful!
You are very welcome. It was an interesting exercise for me. :)
On the face of it, looks like hypertension could be a particular focus. :cool:

Was this using the approximately 16,000 records that you had mentioned earlier?
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,250
Members
452,623
Latest member
Techenthusiast

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top