Announcement

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

  • Looping in Stata

    I work with Stata I need to assign people to income quintiles in each county. So I have a data containing two variables: household total income and a variable "county" which indicate the county of residence (each county corresponds to a certain number like 1, 2, 3, etc.). I use xtile command, like
    xtile quart=tot_income if county==2, nq(10)
    so I create a variable "quart" which indicates in which quintile is the household which resides in "county" numbered 2. Now, the problem is that I have some 1000 counties and I do not want to do it by hand for each one. I though there must be a way to this operation for each county in one command in a loop. I tried "foreach" loop command, but I did not manage to do it. specifically I did:
    1. foreach i in county {
    2. xtile quart=tot_income if county == `i', nq(10)
    3. }
    But no, it did not work, it does the xtile command but for entire sample. Can anybody suggest me what to do?


  • #2
    Some confusion here.

    If you divide into 10 quantile-based bins, then those bins could be called quantile bins (general term) or decile bins (specific term for 10 bins) but the terms quart(ile) (4 bins) and quintile (5 bins) are misleading choices of name. So much for Latin words as used within modern statistical science.

    That aside, many threads here cover divisions into quantile-based bins, but within groups determined by some other variable.

    Code:
    ssc desc egenmore
    points you to an xtile() function of egen you can download and apply.

    Code:
    ssc inst egenmore
    
    egen quant = xtile(tot_income), by(county) nq(10)
    Also cross-posted on Cross Validated, and from there migrated to http://stackoverflow.com/questions/3...oping-in-stata Please see the FAQ Advice for our policy on cross-posting, which is that you should tell us about it.

    Last edited by Nick Cox; 15 Jul 2015, 16:55.

    Comment


    • #3
      Well, thanks for advice. I actually manged to do the loop. The problem was that the variable "quart" one crated during the first loop, was stating the same and I was getting a massage that variable "quart" already exists and loop was terminated. Now I create variable quart1 which contains zeros, and I just add a lines:
      replace quart1=quart if county == `i', nq(10)
      drop quart .
      With this, after I assign values of quart to the quart1 I drop variable quart after each loop and cerate it with each new loop. I do not know if this is the most efficient way, but it works.

      Comment


      • #4
        As my post shows, no loop is needed at all. I am not clear whether you tried my suggestion, but it was based on the information you gave.

        Comment


        • #5
          Dear Stata users,

          My data is in time series format. My end goal is to etimate mean and St.Dev. if each of 100 variables (var_MA22Return_Buy). After that I need to export results to excel.

          I am running the following code:

          forval i=1/100{
          sum var_MA22Return_Buy`i'
          eststo
          }
          esttab using d:\example.csv

          When I open excel file it's empty. Where I went wrong?

          Thank you.

          Comment

          Working...
          X