Announcement

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

  • Creating categorical variables for hypertension

    Hello,

    I am trying to complete a homework assignment for a coding class where I have to "use bpsystol and bpdiast and JNC 7 report to create a categorical variable for bp_group: normal, pre-hypertension, stage 1, stage 2."
    Category Systolic BP (bpsystol) Diastolic BP (bpdiast)
    Normal < 120 < 80
    Pre-hypertension 120 <= bpsystol < 140 80 <= bpdiast < 90
    Stage 1 140 <= bpsystol < 160 90 <= bpdiast < 100
    Stage 2 bpstol >= 160 bpdiast >=100
    I have tried coding this in a couple different ways, but it's not coming out the way I want. If one observation has a systolic bp of 110 but a diastolic bp of 85, they should be categorized as pre-hypertension, but that's not what's happening.

    My most recent attempt used:

    gen bp_group=.
    replace bp_group = 1 if (bpsystol < 120) | (bpdiast < 80)
    replace bp_group = 2 if (inrange(bpsystol, 120, 139)) | (inrange(bpdiast, 80, 89))
    replace bp_group = 3 if (inrange(bpsystol, 140, 159)) | (inrange(bpdiast, 90, 99))
    replace bp_group = 4 if (bpsystol >= 160) | (bpdiast >= 100)
    replace bp_group = . if bpsystol == . | bpdiast == .

    When I used tabstat to check my work, it produced this:

    Click image for larger version

Name:	Screen Shot 2022-04-22 at 7.30.11 PM.png
Views:	2
Size:	63.2 KB
ID:	1661061

    So, the minimum boundary is not being followed in my code. I'm not sure what I'm doing wrong/missing. Please help!
    Attached Files

  • #2
    If one observation has a systolic bp of 110 but a diastolic bp of 85, they should be categorized as pre-hypertension, but that's not what's happening.
    Yes, it is what's happening. Observe:

    Code:
    . * Example generated by -dataex-. For more info, type help dataex
    . clear
    
    . input float(bpsystol bpdiast)
    
          bpsystol    bpdiast
      1. 110 85
      2. end
    
    .
    . gen bp_group=.
    (1 missing value generated)
    
    . replace bp_group = 1 if (bpsystol < 120) | (bpdiast < 80)
    (1 real change made)
    
    . replace bp_group = 2 if (inrange(bpsystol, 120, 139)) | (inrange(bpdiast, 80, 89))
    (1 real change made)
    
    . replace bp_group = 3 if (inrange(bpsystol, 140, 159)) | (inrange(bpdiast, 90, 99))
    (0 real changes made)
    
    . replace bp_group = 4 if (bpsystol >= 160) | (bpdiast >= 100)
    (0 real changes made)
    
    . replace bp_group = . if bpsystol == . | bpdiast == .
    (0 real changes made)
    
    .
    . label define bp_group   1   "Normal"    ///
    >                         2   "Pre-Hypertension"  ///
    >                         3   "Stage 1"   ///
    >                         4   "Stage 2"
    
    . label values bp_group bp_group
    
    .
    . list
    
         +---------------------------------------+
         | bpsystol   bpdiast           bp_group |
         |---------------------------------------|
      1. |      110        85   Pre-Hypertension |
         +---------------------------------------+
    
    .
    And you cannot draw any conclusion from your -tabstat- because blood pressures where systolic and diastolic are in different groups will result in some "out of range" systolics or diastolics being included in the group that that observation ultimately lands in.

    In fact, I am quite confident that your code is correct.

    In the future, please show example data when asking for help with code. And use the -dataex- command to do that. If you are running version 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    Finally, please note that it is the policy here that we do not provide assistance with class homework problems. For that you should consult your teaching assistants, instructor, or, if permitted by your institution and instructor, your classmates. The reason I did respond to this post despite it's being explicitly about homework is that I am limiting my comment to telling you that you have already correctly solved the problem. I am not helping you find the solution. Perhaps that is a bit too technical and I should not have done this, but I think the exception is justified here. However, in the future, please do not post questions from homework assignments. (N.B. Requests relating to theses and dissertations is always welcome. Only class homework problems are excluded.)

    Comment


    • #3
      You are misinterpreting your tabstat results. Those in group 2 with bpsystol<120 have bpdiast>=80, for example.

      Comment


      • #4
        For someone who's ostensibly new to Stata, you've got good intuition on use of Stata's functions and logical operators.

        My only comment here is that the best check of your work here, especially when you're working with Stata's logical operators and doing lots of recoding and such, is the assert command.

        Comment


        • #5
          Thanks, everyone for your help! I will re-run the code and submit.

          Also, I have read more of the FAQs, so I will be more conscientious about what and how I post in future.

          Comment

          Working...
          X