Announcement

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

  • Creating dummy variables by percentiles

    Hello All,

    I'm using a corruption index as an explanatory variable, This index ranks countries from 0 to 100, where 100 is the least corrupt. Instead of using the index in its current form, I'm interested in creating a dummy variable for the 25th percentile. The dummy would take the value of 1 if the country is ranked among the most corrupt (bottom 25%) and 0 otherwise.

    I've found different codes online, but they all seem to be more complex than needed and don't quite work with my data.

    I'd highly appreciate any help.

    Best wishes,
    Henry

  • #2
    Code:
    summ corruption_index, detail
    gen byte dummy = corruption_index <= `r(p25)' if !missing(corruption_index)
    Note: Replace corruption_index by the actual name of your corruption index variable throughout this code. In the future, when asking for help with code, please provide an example of your data, using the -dataex- command to do so. While this example seems straightforward and simple enough, experience on this Forum shows that there is a non-zero chance that this code will fail for reasons having to do with your particular data. For example, people sometimes have their numerical variable actually stored as a string and don't realize it. Or your index may actually be stored as numbers other than 0 to 100, with value labels from 0 to 100 attached. Only by showing actual example data, and using -dataex- to be sure that the relevant metadata is available to those who want to help you, can you assure yourself that the response you get will actually work in your data.

    If you are running version 15.1 or a fully updated version 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


    • #3
      Clyde,

      Thank you very much for your response. It worked perfectly.

      Thanks for your time

      Henry

      Comment

      Working...
      X