Announcement

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

  • "count if any two of the clauses are fulfilled"

    Dear Statalist,

    I am a new member but browsing through your discussions has been a great help many times. Now I have a question myself:

    I wish to count how many of say 300 patients that have two (or three or four…) elevated values in their blood cell counts (among 10 different blood cells) irrespective of which two (three/four/…) blood cells that are elevated.

    So, I am looking for an elegant solution in Stata that says:

    count if any two/three/… of the following clauses are met”, and then you can list a number of different clauses like: “(bloodcell1>25 & bloodcell1<.) | (bloodcell2>.1 & bloodcell2<.) | (bloodcell3>800 & bloodcell3 <.) | ...

    It will be a very long and tedious task to write “count if …” for all possible combinations but is that the only way…?

    Thank you in advance.
    Sabrina

  • #2
    Your example seems hard to generalise but you can count conditions satisfied with something like


    Code:
    gen wanted = inrange(bloodcell1, 25, .) + inrange(bloodcell2, 0.1, .) + inrange(bloodcell3, 800, .)
    and then look for counts of 2 or more.


    Here I am slurring over the fact that inrange() is using intervals like [25, .) which is not the same as (25, .)

    Comment


    • #3
      If my guess is correct, -egenmore-, by Nick Cox, is the brilliant tool to help you out. Install it if you have not done so.
      Code:
      *ssc install egenmore
      egen wanted = rcount(blood*), c(@ > 25 & @ < .)

      Comment


      • #4
        I quite like being called brilliant, so thanks, but I haven't pushed this code (from 2001 and for Stata 6) very hard. For most purposes it was superseded by forvalues and foreach which came in Stata 7.

        Specifically the problem here is that from #1 we say different thresholds for different variables.

        Comment


        • #5
          @Nick, I found -egenmore- really convenient and effective, despite you have built it up a long time ago. And yes, the code in #3 is just my guess for what Sabrina need. After generating wanted, I guess she might want:
          Code:
          count if wanted ==2 // or 3 or 4...
          For me, it would be necessary for Sabrina to add more description. By the way, @ Sabrina, for future posts, please notice FAQ , especially section 12.2 to use -dataex- for giving out a small sample of your data, which would make the discussion more convenient and precise.
          Last edited by Romalpa Akzo; 16 Oct 2018, 07:23.

          Comment


          • #6
            Thank you both very much for your quick and useful answers.

            The solution by @Nick did the trick perfectly, though I am not sure what you mean by the mentioning of [25,.) vs (25,.) intervals?

            @Romalpa, because Nick's solution worked for me, I never got to try yours

            Comment


            • #7
              x in [a, b) means a <= x < b

              x in (a, b] means a < x <= b

              and so on.

              Comment

              Working...
              X