Announcement

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

  • Recoding a new variable based on 2 other variables

    Hi,
    I would like to create a new blood pressure variable with 3 levels based on the following conditions for the variables systolic (BPS) and diastolic (BPD) blood pressure:
    Normal (BPS<120 & BPD<80)
    Prehypertension (BPS=120-139 or BPD=80-89)
    High (BPS=140- >180 or BPD=90- >110)
    Any suggestions would be appreciated.
    Thank you,

    Nick

  • #2
    Several ways to do it, e.g.

    Code:
    gen code = cond(BPS<120 & BPD<80, 1, cond(BPS <= 139 | BPD <= 89, 2, 3)) if !missing(BPS, BPD)
    I don't think anyone would object to 3 lines of code if they seemed clearer.

    Comment


    • #3
      you are losing information by doing this but it can be done:
      Code:
      gen byte bpcat=.
      replace bpcat=0 if BPS<120 & BPD<80
      replace bpcat=1 if bpcat==. & (inrange(BPS,120,139) | inrange(BPD,80,89))
      replace bpcat=2 if bpcat==. & (inrange(BPS,140,180) | inrange(BPD,90,110))
      la define bpcat 0 "Normal" 1 "Prehypertension" 2 "High"
      la val bpcat bpcat
      note that I do not find your question completely clear (e.g., what is the effect of ">" in your definition of "high"?)
      make sure you follow all the above with
      Code:
      ta bpcat, mi

      Comment


      • #4
        Great!
        Thank you both
        Nick

        Comment


        • #5
          I was working on this when Nick & Rich posted their solutions. So rather than discard what I had done, I decided to compare my results with theirs using a small data set I made up. Variable bwcat below is my variable. I used NC and RG as the variable names for Nick & Rich's methods respectively.

          Code:
          clear
          input BPS BPD
          119 79
          120 79
          119 80
          120 80
          139 80
          139 89
          140 89
          139 90
          140 90
          180 110
          181 110
          180 111
          .   100
          115 .
          .   .
          end
          
          * The following assumes BPS and BPD are whole numbers.
          generate bpcat = .
          replace bpcat = 1 if BPS < 120 & BPD < 80
          replace bpcat = 2 if inrange(BPS,120,139) | inrange(BPD,80,89)
          replace bpcat = 3 if inrange(BPS,140,180) | inrange(BPD,90,110)
          replace bpcat = 4 if BPS > 180 | BPD > 110
          replace bpcat = . if missing(BPS) | missing(BPD)
          
          label variable bpcat "BP category"
          label define bplabels 1 "Normal" 2 "Prehypertension" 3 "High" ///
          4 "Hypertensive urgency or emergency"
          *label values bpcat bplabels // Omit value labels for now to make comparison of results easier
          
          * Compare with solutions posted by Nick & Rich.
          
          * Nick Cox (#2)
          gen NC = cond(BPS<120 & BPD<80, 1, cond(BPS <= 139 | BPD <= 89, 2, 3)) ///
          if !missing(BPS, BPD)
          
          * Rich Goldstein (#3):  Make values 1-3 rather than 0-2 to match NC & BW
          gen byte RG=.
          replace RG=1 if BPS<120 & BPD<80
          replace RG=2 if RG==. & (inrange(BPS,120,139) | inrange(BPD,80,89))
          replace RG=3 if RG==. & (inrange(BPS,140,180) | inrange(BPD,90,110))
          list, sep(0)
          Results:
          Code:
          . list, sep(0)
          
               +-----------------------------+
               | BPS   BPD   bpcat   NC   RG |
               |-----------------------------|
            1. | 119    79       1    1    1 |
            2. | 120    79       2    2    2 |
            3. | 119    80       2    2    2 |
            4. | 120    80       2    2    2 |
            5. | 139    80       2    2    2 |
            6. | 139    89       2    2    2 |
            7. | 140    89       3    2    2 |
            8. | 139    90       3    2    2 |
            9. | 140    90       3    3    3 |
           10. | 180   110       3    3    3 |
           11. | 181   110       4    3    3 |
           12. | 180   111       4    3    3 |
           13. |   .   100       .    .    3 |
           14. | 115     .       .    .    . |
           15. |   .     .       .    .    . |
               +-----------------------------+
          I believe that NC and RG are giving incorrect results for observations 7 & 8. Both of these observations meet one of the criteria for high BP, so should be assigned a 3 for High BP. In the case of RG, this could be fixed by dropping RG==. from the if condition.

          Note too that I added a 4th category for cases where BPS > 180 or BPD > 110. I took my cue for that from this website.

          HTH.
          --
          Bruce Weaver
          Email: [email protected]
          Version: Stata/MP 18.5 (Windows)

          Comment


          • #6
            The criteria given by Nikolaos is just an unclear-cut one. Nobody is incorrect, or not? Nick's code is my favorite, since it is 1-line.
            Last edited by Romalpa Akzo; 06 Nov 2017, 16:26.

            Comment


            • #7
              Hi Romalpa. Yes, Nikalaos could have explained things more clearly. But try observations 7 (140/89) and 8 (139/90) in this online calculator. They both convert to High BP.

              Cheers,
              Bruce
              --
              Bruce Weaver
              Email: [email protected]
              Version: Stata/MP 18.5 (Windows)

              Comment


              • #8
                yes, I should have stated that my use of "if bpcat==." is an assumption; dropping that condition changes the results in some cases; only the OP knows for sure what they want the actual result to be

                Comment

                Working...
                X