Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • flagging specific ids with pending lab results

    Hello Stata members,

    I have a dataset with vars (IDs, Disease name, Lab test name, lab test result, and lab status such as (pending, rejected, and final))


    I want to flag those IDs whose requests are still pending uniquely, however, the problem is in the lab status where each row has multiple lab statuses ex (pending, pending, rejected, finely, and final) That is because each ID has multiple tests.


    For your assistance !
    Click image for larger version

Name:	D776AAEF-ABC4-440A-A53B-A09D7978B269.jpeg
Views:	1
Size:	1.27 MB
ID:	1758873



  • #2
    Please use the dataex command to present data examples as recommended in the FAQ Advice.

    Code:
    gen tag= ustrregexm(trim(itrim(lower(teststatus))), "\bpending\b") & length(teststatus)==length("pending")
    or even simply

    Code:
    gen tag= trim(itrim(lower(var1)))== "pending"
    Last edited by Andrew Musau; 16 Jul 2024, 05:34.

    Comment


    • #3
      Thank you Mr. Andrew,

      but I have another issue which is how to make sure those whore pending wre not on the final status as well.

      clear
      input float seq str184 testname str71 teststatus
      1 "monkeypox pcr" "Final"
      1 "IgM Mumps Serology Test" "N/A"
      1 "Measles Viral Isolation (Culture)" "Rejected"
      2 "IgM Measles Serology Test" "Final"
      1 "PCR Bordetella Pertussis" "Final"
      1 "PCR Bordetella Pertussis" "Final"
      1 "IgM Measles Serology Test,IgM Rubella Serology Test" "Final,Final"
      1 "Culture for Bacteria,API,Culture for Diphtheria,Gram Stain,Phoenix,Toxin Production (NA),PCR for Diphtheria toxin gene,PCR for C. diphtheriae,PCR for C. ulcerans" "Final,Rejected,Final,Rejected,Rejected,Rejected,F inal,Final,Final"
      2 "Culture for Bacteria,API,Culture for Diphtheria,Gram Stain,Phoenix,Toxin Production (NA),PCR for Diphtheria toxin gene,PCR for C. diphtheriae" "Final,Rejected,Final,Rejected,Rejected,Rejected,F inal,Final"
      1 "Culture for Salmonella" "Final"
      2 "Culture for Salmonella" "Final"
      1 "IgM Measles Serology Test,IgM Rubella Serology Test,Measles Viral Isolation (Culture)" "Rejected,Final,Rejected"
      2 "IgM Measles Serology Test,IgM Rubella Serology Test,Measles Viral Isolation (Culture)" "Final,Final,Rejected"
      1 "PCR Bordetella Pertussis" "Final"
      1 "IgM Measles Serology Test" "Pending"
      2 "IgM Rubella Serology Test" "Pending"
      1 "Measles Viral Isolation (Culture)" "Final"
      2 "IgM Measles Serology Test" "Final"
      1 "PCR for COVID" "Pending"
      1 "IgM Measles Serology Test,IgM Rubella Serology Test,Measles Viral Isolation (Culture)" "Final,Final,Final"
      2 "IgM Measles Serology Test,IgM Rubella Serology Test,Measles Viral Isolation (Culture)" "Final,Final,Final"
      1 "IgM Measles Serology Test,IgM Rubella Serology Test,Measles Viral Isolation (Culture)" "Final,Final,Final"
      1 "PCR Bordetella Pertussis" "Final"
      1 "Respiratory multiplex PCR
      " "Pending"
      1 "PCR Bordetella Pertussis" "Final"
      1 "PCR Bordetella Pertussis" "Final"
      1 "PCR Bordetella Pertussis" "Final"
      1 "PCR Bordetella Pertussis" "Final"
      1 "PCR Bordetella Pertussis" "Final"
      1 "ITD-P1,ITD-P2,ITD-P3,Polio Isolation" "Final,Final,Final,Final"
      2 "ITD-P1,ITD-P2,ITD-P3,Polio Isolation" "Final,Final,Final,Final"
      1 "PCR Bordetella Pertussis" "Final"
      1 "PCR Bordetella Pertussis" "Final"
      1 "PCR Bordetella Pertussis" "Final"
      1 "PCR Bordetella Pertussis" "Final"
      1 "IgM Measles Serology Test" "Final"
      1 "ITD-P1,Polio Isolation" "Final,Final"
      1 "IgM Measles Serology Test" "Final"
      2 "Measles Viral Isolation (Culture)" "Pending"
      1 "IgM Measles Serology Test" "Final"
      2 "Measles Viral Isolation (Culture)" "Rejected"
      1 "IgM Measles Serology Test" "Final"
      2 "Measles Viral Isolation (Culture)" "Rejected"
      1 "Culture for Pertusis,PCR Bordetella Pertussis,Serology" "Rejected,Final,Rejected"
      1 "Culture for Pertusis,PCR Bordetella Pertussis,Serology" "Rejected,Final,Rejected"
      1 "IgM Measles Serology Test,IgM Rubella Serology Test,Measles Viral Isolation (Culture)" "Final,Final,Rejected"
      1 "Culture for Pertusis,PCR Bordetella Pertussis,Serology" "Rejected,Final,Rejected"
      1 "Culture for Pertusis,PCR Bordetella Pertussis,Serology" "Rejected,Final,Rejected"
      1 "Culture for Pertusis,PCR Bordetella Pertussis,Serology" "Rejected,Final,Rejected"
      1 "Culture for Pertusis,PCR Bordetella Pertussis,Serology" "Rejected,Final,Rejected"
      1 "IgM Mumps Serology Test" "Final"
      1 "IgM Measles Serology Test,IgM Rubella Serology Test,Measles Viral Isolation (Culture)" "Rejected,Final,Rejected"
      2 "IgM Measles Serology Test,IgM Rubella Serology Test,Measles Viral Isolation (Culture)" "Final,Final,Final"
      1 "IgM Measles Serology Test,IgM Rubella Serology Test,Measles Viral Isolation (Culture)" "Final,Final,Final"
      2 "IgM Measles Serology Test,IgM Rubella Serology Test,Measles Viral Isolation (Culture)" "Final,Final,Final"
      1 "IgM Measles Serology Test" "Final"
      1 "Measles Viral Isolation (Culture)" "Rejected"
      2 "IgM Measles Serology Test" "Final"
      1 "Stool culture" "Preliminary"
      1 "Blood Culture" "Final"
      2 "Stool culture" "Final"
      1 "Measles Viral Isolation (Culture)" "Rejected"
      2 "IgM Measles Serology Test,IgM Rubella Serology Test" "Final,Final"
      3 "ITD-P1,ITD-P2,ITD-P3,Polio Isolation" "Final,Final,Final,Final"
      4 "ITD-P1,ITD-P2,ITD-P3,Polio Isolation" "Final,Final,Final,Final"
      1 "IgM Measles Serology Test" "Final"
      1 "Dengue IgM,Dengue IgG,Dengue PCR" "Pending,Pending,Pending"
      1 "Stool culture" "Final"
      1 "IgM Mumps Serology Test" "Final"
      1 "Culture for Pertusis,PCR Bordetella Pertussis,Serology" "Final,Final,Final"
      1 "PCR for COVID" "Pending"
      1 "PCR for COVID" "Pending"
      1 "PCR for COVID" "Pending"
      1 "IgM Rubella Serology Test,IgM Measles Serology Test,Measles Viral Isolation (Culture)" "Final,Final,Final"
      2 "monkeypox pcr" "Pending"
      1 "PCR for COVID" "Pending"
      1 "Botulinum Toxin Test" "Final"
      2 "Botulinum Toxin Test" "Final"
      3 "Botulinum Toxin Test,Culture for Botulinum" "Final,Final"
      1 "PCR for COVID" "Pending"
      1 "Al Khurma PCR,Chikungunya PCR,Dengue IgG,Dengue IgM,Dengue NS1,Dengue PCR,Rift Valley Fever PCR" "Pending,Pending,Pending,Pending,Pending,Pending,P ending"
      1 "IgM Measles Serology Test,Measles Viral Isolation (Culture)" "Final,Final"
      2 "IgM Measles Serology Test,Measles Viral Isolation (Culture),IgM Rubella Serology Test" "Final,Final,Final"
      3 "IgM Mumps Serology Test" "Final"
      1 "PCR for COVID" "Pending"
      1 "PCR for COVID" "Pending"
      2 "PCR for COVID" "Pending"
      1 "PCR for Influenza" "Pending"
      1 "PCR for COVID" "Pending"
      1 "PCR for COVID" "Pending"
      1 "IgM Measles Serology Test,IgM Rubella Serology Test,Measles Viral Isolation (Culture)" "Final,Final,Rejected"
      1 "PCR for Influenza" "Pending"
      1 "PCR for COVID" "Pending"
      1 "PCR for COVID" "Pending"
      1 "Culture for Pertusis" "Final"
      1 "PCR for COVID" "Pending"
      1 "PCR for COVID" "Pending"
      1 "PCR for COVID" "Pending"
      1 "PCR for COVID" "Pending"
      1 "PCR for COVID" "Pending"
      end

      Comment


      • #4
        Is "seq" an individual identifier?

        Code:
        bys seq: egen final=max(ustrregexm(lower(teststatus), "\bfinal\b"))
        bys seq: egen pending=max(ustrregexm(lower(teststatus), "\bpending\b"))
        gen tag= pending&!final
        In the above, any entry of pending is tagged. If you want observations with a unique instance of pending, change the second line to

        Code:
        bys seq: egen pending=max(trim(itrim(lower(teststatus)))== "pending")
        For a discussion of the technique, see https://www.stata.com/support/faqs/d...ble-recording/.

        Comment


        • #5
          Many thanks again Mr. Andrew,

          Yes, it is.

          It flags the pending and the final but there is a problem that remains unsolved which is:
          After flagging the IDs that are pending, I also want to make sure those IDs do not have other final tests in the dataset, if so I want to remove them from the pending flags list.

          Comment


          • #6
            That's what the indicator "tag" does.

            gen tag= pending&!final
            To drop observations, simply proceed with

            Code:
            keep if tag
            Otherwise, show an example where the code does not do what you expect it to.

            Comment


            • #7
              Sorry for the late reply,

              I used the below coding

              Step 1: Create a flag for 'final' status
              gen final_flag = (ustrregexm(lower(teststatus), "\bfinal\b"))

              * Step 2: Create a new variable to indicate if an ID has any 'final' status
              bys id: egen has_final = max(final_flag)

              * Step 3: Assign the final classification
              gen final_classification = ""
              replace final_classification = "final" if has_final == 1
              replace final_classification = "pending" if has_final == 0

              * Step 4: Optionally, list the results
              list id teststatus final_classification, sepby(id)
              and it worked

              Comment

              Working...
              X