Announcement

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

  • Creating loop over percentiles with egen

    Dear Statalist,

    I am new to Stata and trying to create a loop. I am looking at lifetime earnings, and want to be able to plot and get values for specific quantiles at each age.

    Here is the code I have tried:

    foreach pct of varlist 10 20 30 50 70 80 {
    egen female`pct'=pctile(salary) if female==1 & unigrad==1, p(`pct') by(age)
    }

    To which Stata returns "invalid name"

    Similarly, I am using the user written ado file "rifreg", and would like to run it over the same quantiles again by gender.
    Running the following:

    qui rifreg income age age2 age3 age4 if age>21 & age<60 & female==1 & unigrad==1, quantile(.10)
    predict fem10
    qui rifreg income age age2 age3 age4 if age>21 & age<60 & female==1 & unigrad==1, quantile(.20)
    predict fem20

    ect. has worked, but I would like to loop it also to save time.
    I have tried the following to the same error message:

    foreach pct of varlist 10 20 30 50 70 80 {
    qui rifreg income age age2 age3 age4 if age>21 & age<60 & female==1 & unigrad==1, quantile(`pct')
    predict fem`pct'
    }

    Best,
    Gabrielle

  • #2
    In this code

    Code:
    foreach pct of varlist 10 20 30 50 70 80 {
         egen female`pct'=pctile(salary) if female==1 & unigrad==1, p(`pct') by(age)
    }
    the problem is that 10 20 30 50 70 80 is not a varlist, meaning a list of existing variable names. It's not a list of legal new variable names either. Hence Stata is choking as soon as it sees the 10 , which is not a valid name.

    Code:
    foreach pct in 10 20 30 50 70 80 {
    should work.

    Comment


    • #3
      Thank you very much Nick!

      Comment

      Working...
      X