Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: st: panel data, sorting in deciles
From
Maarten buis <[email protected]>
To
[email protected]
Subject
Re: st: panel data, sorting in deciles
Date
Mon, 11 Oct 2010 10:57:19 +0100 (BST)
--- On Sun, 10/10/10, Biljana Dlab wrote:
> . foreach X of varlist dat* {
> 2. xtile deciles=`X', n(10)
> 3. }
> deciles already defined
>
> why did I get "deciles already defined" message in red.
-xtile deciles- creates a new variable called deciles.
However, because you are doing this inside a loop, you
are trying to do this multiple times. Lets say there are
3 dat* variables dat1, dat2, and dat3. In that case
your loop tries to do the following:
step 1) For dat1 create a variable deciles containing the
deciles of dat1
step 2) For dat2 create a variable deciles containing the
deciles of dat2
step 3) For dat3 create a variable deciles containing the
deciles of dat3
At step 2) -xtile- tries to create the variable deciles,
but that variable was already created in step 2, so Stata
complains and quits the loop.
In order to make such a loop work you need to make sure
that at each step it creates a variable with a different
name. One way of doing that is as follows (I don't like
capital letters in my code, it means more typing and more
chance of a typo/bug, but that is my personal preference):
foreach x of varlist dat* {
xtile d_`x'=`x', n(10)
}
Hope this helps,
Maarten
--------------------------
Maarten L. Buis
Institut fuer Soziologie
Universitaet Tuebingen
Wilhelmstrasse 36
72074 Tuebingen
Germany
http://www.maartenbuis.nl
--------------------------
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/