Announcement

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

  • Convert blood pressure data from string to numeric

    I have an issue with STATA where I am not able to successfully convert blood pressure string variable (ex. 120/80) into numeric?

  • #2
    Should be straightforward. Have you tried splitting on `/` and then destringing if needed?
    https://www.stata.com/manuals13/dsplit.pdf
    https://www.stata.com/manuals13/ddestring.pdf
    Thank you for your help!

    Stata SE/17.0, Windows 10 Enterprise

    Comment


    • #3
      You cannot convert this into a single numeric variable, because it involves two distinct numbers. You can, however, break it up into two numeric variables, one each for systolic and diastolic blood pressure:

      Code:
      split string_bp_var, gen(bp) parse("/") destring
      rename bp1 sbp
      rename bp2 dbp
      Added: Crossed with #2 who points you to the same solution.

      Comment


      • #4
        Thank you. Pratap and Clyde. This is helpful!!!

        Comment


        • #5
          Reviving this thread to add on a question.

          I have patient encounter level data, with each encounter having 1 or more (range 1-62) number of blood pressuring readings in a single, comma-separated field.

          Code:
          * Example generated by -dataex-. To install: ssc install dataex
          clear
          input str472 vitalsbp
          " 139/82, 146/65"
          " 157/95"        
          " 113/78"        
          " 114/68"        
          " 141/74"        
          " 133/92, 144/83"
          " 140/77"        
          " 107/62"        
          " 139/82"        
          " 120/82"        
          end

          I split this into separate readings using:
          Code:
           split vitalsbp, parse(,) generate(vitals_bp)
          and now have vitals_bp1 - vitals_bp62 variables.


          Now I need to split each BP reading into separate systolic and diastolic readings, and I'm stuck. You can't use the split command on more than one variable, it seems. Do I need to write a loop or is there a simpler solution? Thanks.

          Comment


          • #6
            Code:
            foreach v of var  vitals_bp1 - vitals_bp62 { 
                 split `v', gen(`v'_) parse("/") destring 
            }
            You could add some renaming, e.g. to systolic, diastolic as suffix or prefix.

            Comment


            • #7
              Thanks much, Nick. That loop worked. I then used two loops to rename the resulting variables as follows:

              Code:
              foreach v of var  vitals_bp*_1 { 
                   rename `v' `v'_systolic 
              }
              
              foreach v of var  vitals_bp*_2 { 
                   rename `v' `v'_diastolic 
              }
              And ended up with variables names as follows:
              bp_1_1_systolic
              bp_1_2_diastolic
              bp2_1_systolic
              bp2_2_diastolic
              etc

              But now I'm trying to generate variables based on these variables and running into trouble again. I tried the following gen commands but am getting a "vitals_bp ambiguous definition" error. I tried a loop instead but that gives me a "type mismatch" error.
              Code:
              gen abnl = (vitals_bp*_1_systolic <100)
              
              gen abnl = (vitals_bp*_*_systolic <100)
              
              
              foreach v of var vitals_bp*_1_systolic { 
                   replace abnl=1 if `v' <100 
              }

              Comment


              • #8
                What you want to do?

                Count how often systolic is less than 100?

                That could be

                Code:
                gen count_sysLT100 = 0 
                
                qui foreach v of var *systolic { 
                    replace count_sysLT100 = count_sysLT100 + (`v' < 100) 
                }
                An indicator for any being under 100 would be count_sysLT100 > 0

                Comment


                • #9
                  Thanks again, Nick. I was trying to generate indicator variables for abnormal vital signs. Silly mistake on my end; I didn't notice that the first BP variable generated from the parsing loop was a string, hence the "type mismatch" error. Once I fixed that, this loop worked.

                  Code:
                  qui foreach v of var *systolic {
                        replace abnl=1 if `v' <100
                   }
                  Last edited by Will Fleischman; 22 May 2023, 14:26.

                  Comment

                  Working...
                  X