Announcement

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

  • Loop for Blood pressure recodification

    Hi all,

    I have some data of Blood pressure in pediatric data, my task is recode Blood pressure in:

    1=Hipotension (below range)
    2=Normal (range in data table)
    3=Hypertension (up range)

    To do this I have a table with multples "if" to make the recodification because range depending of sex and age in my example. I attach the table but also you can see the link: https://medicine.uiowa.edu/iowaproto...-normal-ranges.

    I do the recodification in a normal way (no loop), but I wonder if its possible make this with a loop (I tried but I failed...). Maybe some clue to I complete the loop code...
    Example

    Code:
    clear all
    set more off
    webuse nhanes2
    
    * dataset example its not for pediatric data, so I make some modifications:
    
    gen age2=age-20
    drop if age2 >13
    sum age2
    
    
    Code:
        Variable |       Obs        Mean    Std. Dev.       Min        Max
    -------------+--------------------------------------------------------
            age2 |      3055    5.986579    3.972171          0         13
    tab sex
    Code:
        1=male, |
       2=female |      Freq.     Percent        Cum.
    ------------+-----------------------------------
           Male |      1,469       48.09       48.09
         Female |      1,586       51.91      100.00
    ------------+-----------------------------------
          Total |      3,055      100.00
    * diastolic pressure recodification gen diastpr_recode=bpdiast // diastolic pressure recodification // girls // 0 AGE recode diastpr_recode /// (min/36=1 ) /// (37/56=2 ) /// (57/max=3) /// if sex==2 & age2==0 // 1 AÑO recode diastpr_recode /// (min/41=1 ) /// (42/58=2 ) /// (59/max=3) /// if sex==2 & age2==1 // 2 AÑO recode diastpr_recode /// (min/47=1 ) /// (48/62=2 ) /// (63/max=3) /// if sex==2 & age2==2 // 3 AÑO recode diastpr_recode /// (min/49=1 ) /// (50/65=2 ) /// (66/max=3) /// if sex==2 & age2==3 ***... I completed all years until girls 13 years old // diastolic pressure: // boys ////////////////////// // 0 AGE recode diastpr_recode /// (min/55=1 ) /// (37/56=2 ) /// (57/max=3) /// if sex==1 & age2==0 // 1 AÑO recode diastpr_recode /// (min/40=1 ) /// (41/54=2 ) /// (55/max=3) /// if sex==1 & age2==1 // 2 AÑOS recode diastpr_recode /// (min/43=1 ) /// (44/58=2 ) /// /// (59/max=3) /// if sex==1 & age2==2 // 3 AÑOS recode diastpr_recode /// (min/46=1 ) /// (47/61=2 ) /// (62/max=3) /// if sex==1 & age2==3 ***... I completed all years until boys 13 years old label define diastpr_recode 1 "Hipotension" 2 "Normal" 3 "Hypertension" label value diastpr_recode diastpr_recode
    Click image for larger version

Name:	tabla.png
Views:	1
Size:	36.8 KB
ID:	1605833

    Last edited by Rodrigo Badilla; 25 Apr 2021, 20:35.

  • #2
    9

    Comment


    • #3
      This can be considerably simplified. First, a little playing around with the table at the link enables the creation of a Stata data set that contains the normal ranges:
      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input byte age long sex int(syst_bp1 syst_bp2) byte(diast_bp1 diast_bp2)
       0 2  72 104 37 56
       0 1  72 104 37 56
       1 2  85 102 42 58
       1 1  86 101 41 54
       2 2  89 106 48 62
       2 1  89 104 44 58
       3 2  90 107 50 65
       3 1  90 105 47 61
       4 2  92 108 53 67
       4 1  92 107 50 64
       5 2  93 110 55 70
       5 1  94 110 53 67
       6 2  91 108 59 73
       6 1  90 109 59 73
       7 2  92 110 60 74
       7 1  91 111 60 74
       8 2  94 112 60 75
       8 1  93 113 60 75
       9 2  95 114 61 76
       9 1  94 115 61 75
      10 2  97 116 62 77
      10 1  96 117 62 76
      11 2  99 118 63 78
      11 1  98 119 62 77
      12 2 100 120 64 78
      12 1 100 121 63 78
      13 2 102 121 64 79
      13 1 102 124 64 80
      end
      label values sex sex
      label def sex 1 "Male", modify
      label def sex 2 "Female", modify
      So, let me assume that you have saved this data set as bp_normal_ranges.dta.

      Now, I assume that each observation in your original data contains a variable called age (numeric, coded 0 for anything < 1 year), and a variable called sex coded 1 for Male and 2 for Female. And there is another variable systolic_bp, and another, diastolic_bp. With that:

      Code:
      use my_data, clear
      merge m:1 age sex using bp_normal_ranges
      gen systolic_category = 1 if systolic_bp < syst_bp1
      replace systolic_category = 2 if inrange(systolic_bp, syst_bp1, syst_bp2)
      replace systolic_category = 3 if systolic_bp > syst_bp2 & !missing(systolic_bp)
      gen diastolic_category = 1 if diastolic_bp < diast_bp1
      replace diastolic_category = 2 if inrange(diastolic_bp, diast_bp1, diast_bp2)
      replace diastolic_category = 3 if diastolic_bp > diast_bp2 & !missing(diastolic_bp)
      After that, if you want to clean up a little bit, you can drop syst_bp* and diast_bp* if you won't need those limits again. And you can put a value label on systolic_category and diastolic_category if you like.

      Notice that no loops are required for this. (You could combine the last 6 lines into a loop if you wanted, but I think it's easier to see what's going on this way.)

      In the future, please show example data using the -dataex- command when you are asking for help with code. 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.
      Last edited by Clyde Schechter; 25 Apr 2021, 21:08.

      Comment


      • #4
        Thanks Clyde Schechter for you elegant solution...

        I guest that you could help me.

        I will work with your command.

        Regards
        Rodrigo

        Comment


        • #5
          Clyde Schechter I tried to use dataex, but failed, next time i will try again...

          Just 2 questions:

          1) sex its included in each recodification? I could not see sex in replace only in merge...

          2) Why syst is "int" and diast is "byte", why are differents?:
          input byte age long sex int(syst_bp1 syst_bp2) byte(diast_bp1 diast_bp2)
          Last edited by Rodrigo Badilla; 25 Apr 2021, 21:32.

          Comment


          • #6
            One way is to turn it into a program:

            Code:
            * Generate fake data to demonstrate:
            clear
            set seed 77221
            set obs 100
            gen sex  = floor(runiform() > 0.5) + 1
            gen age2 = floor(runiform() * 14)
            gen bpdiast = round(rnormal(68, 2),0.1)
            gen bpsysto = round(rnormal(100, 10),0.1)
            
            * Create the indicator
            gen diastpr_recode = .
            gen systopr_recode = .
            * Cap age at 13 if it's >= 13
            gen agecap13 = age2
            replace agecap13 = 13 if age2 >= 13 & age2 < .
            
            * Program:
            capture program drop BPR
            program define BPR
            replace systopr_recode = 1 if bpsysto < `3' &               sex == `1' & agecap13 == `2'
            replace systopr_recode = 2 if inrange(bpsysto, `3', `4') &  sex == `1' & agecap13 == `2'
            replace systopr_recode = 3 if bpsysto > `4' & bpsysto < . & sex == `1' & agecap13 == `2'
            replace diastpr_recode = 1 if bpdiast < `5' &               sex == `1' & agecap13 == `2'
            replace diastpr_recode = 2 if inrange(bpdiast, `5', `6') &  sex == `1' & agecap13 == `2'
            replace diastpr_recode = 3 if bpdiast > `6' & bpdiast < . & sex == `1' & agecap13 == `2'
            end
            
            * Run the program BPR, provide the data in the following order:
            * 1st: Sex code (sex)
            * 2nd: Age code (agecap13)
            * 3rd: Syst lower bound
            * 4th: Syst upper bound
            * 5th: Dias lower bound
            * 6th: Dias upper bound
            BPR 2 0 72 104 37 56
            BPR 2 1 85 102 42 58
            BPR 2 2 89 106 48 62
            BPR 2 3 90 107 50 65
            BPR 2 4 92 108 53 67
            BPR 2 5 93 110 55 70
            BPR 2 6 91 108 59 73
            * So on, so forth
            BPR 2 12 100 120 64 78
            BPR 2 13 102 121 64 79
            
            * Take a look:
            sort agecap13
            list if diastpr_recode != ., sepby(agecap13)
            Results:
            Code:
                 +-----------------------------------------------------------------+
                 | sex   age2   bpdiast   bpsysto   diastp~e   systop~e   agecap13 |
                 |-----------------------------------------------------------------|
              1. |   2      0      69.6      94.3          3          2          0 |
              2. |   2      0      67.5     100.1          3          2          0 |
              3. |   2      0      65.4     106.6          3          3          0 |
              5. |   2      0      67.8      93.6          3          2          0 |
              6. |   2      0      67.6      93.8          3          2          0 |
                 |-----------------------------------------------------------------|
              7. |   2      1      65.7     104.6          3          3          1 |
              9. |   2      1      68.4      94.9          3          2          1 |
             11. |   2      1      68.3        95          3          2          1 |
             12. |   2      1      68.8     102.4          3          3          1 |
             14. |   2      1      66.2      94.1          3          2          1 |
             15. |   2      1      67.2      96.3          3          2          1 |
                 |-----------------------------------------------------------------|
             16. |   2      2      70.1     110.1          3          3          2 |
             17. |   2      2      68.5     103.8          3          2          2 |
             21. |   2      2      68.2      88.1          3          1          2 |
             22. |   2      2      68.4     117.7          3          3          2 |
             23. |   2      2      70.9      85.2          3          1          2 |
             24. |   2      2      67.6      91.1          3          2          2 |
             25. |   2      2      66.3     103.3          3          2          2 |
                 |-----------------------------------------------------------------|
             26. |   2      3      67.6     101.6          3          2          3 |
             27. |   2      3      68.6      91.9          3          2          3 |
             28. |   2      3      69.2     112.6          3          3          3 |
             32. |   2      3        67     107.3          3          3          3 |
             33. |   2      3      66.3     109.6          3          3          3 |
                 |-----------------------------------------------------------------|
             35. |   2      4      65.4     105.4          2          2          4 |
             38. |   2      4        67      87.8          2          1          4 |
             41. |   2      4      66.3     102.5          2          2          4 |
             42. |   2      4      67.2     104.5          3          2          4 |
                 |-----------------------------------------------------------------|
             44. |   2      5      67.4      83.4          2          1          5 |
             45. |   2      5      69.5     102.5          2          2          5 |
             49. |   2      5      70.2      90.1          3          1          5 |
             50. |   2      5      70.1      91.7          3          1          5 |
             51. |   2      5      69.6      89.6          2          1          5 |
                 |-----------------------------------------------------------------|
             54. |   2      6      68.7     106.4          2          2          6 |
             55. |   2      6      65.7      86.9          2          1          6 |
             57. |   2      6      65.3      97.8          2          2          6 |
                 |-----------------------------------------------------------------|
             97. |   2     13      67.7     107.5          2          2         13 |
             98. |   2     13      69.1      99.5          2          1         13 |
                 +-----------------------------------------------------------------+
            To learn more, use -help program-

            Comment


            • #7
              1) sex its included in each recodification? I could not see sex in replace only in merge...
              No, sex is not needed in the recodificdation, because the values of syst_bp1 syst_bp2 diast_bp1 and diast_bp2 are already sex-specific. Look at the bp_normal_ranges data set: each observation corresponds to a single age and sex. The recodficiation process only uses the limits for that person's age and sex.

              2) Why syst is "int" and diast is "byte", why are differents?:
              The maximum number that can be stored in a byte is 100. All of the diastolic thresholds are less than that, so the diast_* fit into a byte. But some of the syst_* are greater than 100, so int, which is a larger storage type, is needed there. If, for some reason, this makes you uncomfortable, you can make them all int. (You can't make them all byte, because some of the syst_* are too big to fit in a byte.)

              Comment


              • #8
                Thanks Clyde Schechter for your time. All clear .

                Thanks Ken Chui for your program, I will check with my data.

                Regards
                Last edited by Rodrigo Badilla; 25 Apr 2021, 22:29.

                Comment

                Working...
                X