Announcement

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

  • Generate new variable and assign values for >=,< mean

    I am new to stata and I have tried so many options, I have tried watching videos, reading manuals and guidebooks and I cannot figure out how to do this. I'm getting so frustrated by it!

    the data is http://www.stata-press.com/data/mlmus4/bp

    and I need to figure out how to generate a new variable for bp and assign values of 0 if they are lower than the mean of bp and the value of 1 if bp values are equal or greater than the mean of bp

    I am really struggling

    Thank you in advance for your help
    Last edited by Catherine Butler; 16 Nov 2022, 14:41.

  • #2
    The link you posted is broken, so I cannot see the data set.

    Nevertheless, on the assumption that you have a variable named bp, and that it is a true numeric variable:
    Code:
    summarize bp, meanonly
    local mean_bp = r(mean)
    gen byte wanted = bp >= `mean_bp' if !missing(bp)

    Comment


    • #3
      thank you so much!
      Last edited by Catherine Butler; 16 Nov 2022, 15:29.

      Comment


      • #4
        Yes, that would be appropriate data for that code.

        In the future, when showing data examples, please use the -dataex- command to do so. 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.

        When asking for help with code, always show example data. When showing example data, always use -dataex-.

        Comment


        • #5
          okay wonderful thank you! I will do that in the future sorry I'm new.

          Thanks again

          Comment

          Working...
          X