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: Foreach looping code for generating a Quantiles variable for each two digit sic code
From
Nick Cox <[email protected]>
To
[email protected]
Subject
Re: st: Foreach looping code for generating a Quantiles variable for each two digit sic code
Date
Thu, 16 Feb 2012 22:02:28 +0000
The trickiest part here is that years are represented inconsistently
but otherwise the loop is just a loop over years. Study of -help
numlist- points to a more direct solution:
foreach y of num 95/99 2000/2010 {
egen resremRDq`y' = xtile(resremRD) if Year ==`v' ,
nquantiles(4) by(twodigit)
}
Naturally all you need to do is
replace Year = Year + 1900 if Year < 100
and then the loop could just be
foreach y of num 1995/2010 {
egen resremRDq`y' = xtile(resremRD) if Year ==`v' ,
nquantiles(4) by(twodigit)
}
or
forval y = 1995/2010 {
egen resremRDq`y' = xtile(resremRD) if Year ==`v' ,
nquantiles(4) by(twodigit)
}
However, why use separate variables for each year when you could put
the results in one variable?
egen resremRDq = xtile(resremRD), nquantiles(4) by(twodigit year)
There is a basic tutorial on loops in
SJ-2-2 pr0005 . . . . . . Speaking Stata: How to face lists with fortitude
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
Q2/02 SJ 2(2):202--222 (no commands)
demonstrates the usefulness of for, foreach, forvalues, and
local macros for interactive (non programming) tasks
The strange but to me pleasing fact that the work appeared in 2(2):
202-222 (2002) was a pure accident, but too too droll not to mention.
Back to your code: The loop
foreach t of loc ts {
xtile resremRDq`yr' = resremRD`yr', nquantiles(4)
}
fails because you make no use of the values of -ts- within the loop.
Thus second time round the loop the -xtile- call is exactly the same
as first time round, and your code stops within each error. I guess
you omitted an -if- condition.
Nick
On Thu, Feb 16, 2012 at 9:14 PM, Jaime Laird <[email protected]> wrote:
> I have search the appropriate places and have not seen my particular question addressed so I hoping I will add something to the list with this question. Forgive me if I have missed something.
>
> What I need to do:
>
> I need to plot the Quartiles for each two-digit Siccode in my data and it also need to be done by year. I have successfully found the code to do this but I would like to better understand looping to streamline tasks such as these.
>
> The following code gives the results I need (I downloaded this SSC from within Stata, it is called egenmore):
>
> egen resremRDq96 = xtile (resremRD), by (twodigit), if Year ==96, nquantiles(4)
> egen resremRDq97 = xtile (resremRD), by (twodigit), if Year ==97, nquantiles(4)
> egen resremRDq98 = xtile (resremRD), by (twodigit), if Year ==98, nquantiles(4)
> egen resremRDq99 = xtile (resremRD), by (twodigit), if Year ==99, nquantiles(4)
> egen resremRDq2000 = xtile (resremRD), by (twodigit), if Year ==2000, nquantiles(4)
> egen resremRDq2001 = xtile (resremRD), by (twodigit), if Year ==2001, nquantiles(4)
> egen resremRDq2002 = xtile (resremRD), by (twodigit), if Year ==2002, nquantiles(4)
> egen resremRDq2003 = xtile (resremRD), by (twodigit), if Year ==2003, nquantiles(4)
> egen resremRDq2004 = xtile (resremRD), by (twodigit), if Year ==2004, nquantiles(4)
> egen resremRDq2005 = xtile (resremRD), by (twodigit), if Year ==2005, nquantiles(4)
> egen resremRDq2006 = xtile (resremRD), by (twodigit), if Year ==2006, nquantiles(4)
> egen resremRDq2007 = xtile (resremRD), by (twodigit), if Year ==2007, nquantiles(4)
> egen resremRDq2008 = xtile (resremRD), by (twodigit), if Year ==2008, nquantiles(4)
> egen resremRDq2009 = xtile (resremRD), by (twodigit), if Year ==2009, nquantiles(4)
>
> A member here gave me a code to help loop my regressions and I tried to adapt it to work with these quantiles but was unsuccessful. Here is the code I tried to make from his:
>
> forv y=1995/2009 {
> if `y'<2000 loc yr=substr("`y'",3,2)
> else loc yr `y'
> qui levelsof twodigit if !mi(rd`yr'), loc(ts)
> foreach t of loc ts {
> xtile resremRDq`yr' = resremRD`yr', nquantiles(4)
> }
> }
>
> The results only formed one new variable named "resremrdq95" and it did not assign the Quartiles by siccode (twodigit).
>
> It also returns this error:
>
> run "/var/folders/aL/aLs0EaEHFfCArViD4qvSeU+++TI/-Tmp-//SD07955.000000"
> resremRDq95 already defined
> r(110);
>
> I truly want to learn so please be patient with me. I am not simply looking for quick fixes. I want to be able to write my own codes effectively.
> In my mind, the above code should work because it is telling stata to look at each year in 'y', through the levels of twodigit. What am I missing?
*
* 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/