Announcement

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

  • Percentile variable to continous variable

    Hello! i need to run a reggresion with a variable that is in percentiles, i tried with foreach x for variable and also tried standarized it but it wont work. This is a variable p311t1 of the total spending in a household:

    type: numeric (long)

    range: [2,10000] units: 1
    unique values: 226 missing .: 581,181/581,717

    mean: 661.39
    std. dev: 1067.87

    percentiles: 10% 25% 50% 75% 90%
    40 100 300 720 1500

    here is the code:
    Code:
    foreach v in p311t1{
        summ     `v'
        gen     gast_`v'= (`v'-r(mean))/r(sd)
    }
    d gast_
    egen gasto = rowtotal(gast_)
    summ gasto
    gen gasto_ = (gasto-r(mean))/r(sd)
    sum gasto_
    please leave your suggestions, thank you

  • #2
    I'm not sure I follow. If you just want a variable that is percentile values
    Code:
    xtile newvar= p311t1, nq(100)

    If you want to rescale your variable to 0-100 then
    Code:
    sum p311t1
    gen newvar=(p311t1-r(min))*100/(r(max)-r(min))

    hth,
    Jeph

    Comment


    • #3
      There is no need to make a loop for a single variable, though it does no harm. I don't see anything wrong with your code. And while it is suspicious that the variable p311t1 has so many missing values, nevertheless I don't see why your code would not have produced the standardized variable you want. As you also don't say what happened other than it "didn't work" there really is nothing to go on to help you here.

      You should be able to get a standardized version of p31151 with the following code:
      Code:
      summ p311t1
      gen gast_p311t1 = (p311t1 - r(mean))/r(sd)
      If that does not work, please post back with:

      1. Example data that reproduces the problem you are having. 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.

      2. Show the output that Stata is giving you in the Results window. Show it exactly and completely by copying it to your clipboard and pasting it into the forum editor between code delimiters. Be sure to include any messages from Stata in this.

      3. If you are getting a variable gast_p311t1 but you believe its values are incorrect, then show example data that includes gast_p311t1, and unless it is blatantly obvious, explain why what you are getting differs from what you were expecting.

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

      Added: crossed with #2

      Comment


      • #4
        Thank you so much, it worked!

        Comment

        Working...
        X