Notice: On March 31, it was **announced** that Statalist is moving from an email list to a **forum**. The old list will shut down on April 23, and its replacement, **statalist.org** is already up and running.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

From |
Jaime Laird <jmc981@gmail.com> |

To |
statalist@hsphsun2.harvard.edu |

Subject |
Re: st: Foreach looping code for generating a Quantiles variable for each two digit sic code |

Date |
Thu, 16 Feb 2012 16:19:41 -0600 |

Nick, Thank you for the reply! That was very helpful. Yes, I did omit an -if- condition from his original code. I also forgot to mention that my code I listed below was set up to work with the data in long format when I had a Year variable but I was hoping to be able to loop the command without switching to long. One question: Where does the `v' come in? What does it mean? > egen resremRDq`y' = xtile(resremRD) if Year ==`v' , > 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) **This is exactly the end result I wanted! Thank you for this. I assume I will need to format long to get the year variable to use this also? On Feb 16, 2012, at 4:02 PM, Nick Cox wrote: > 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 <jmc981@gmail.com> 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/ * * 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/

**Follow-Ups**:**Re: st: Foreach looping code for generating a Quantiles variable for each two digit sic code***From:*Nick Cox <njcoxstata@gmail.com>

**References**:**st: Foreach looping code for generating a Quantiles variable for each two digit sic code***From:*Jaime Laird <jmc981@gmail.com>

**Re: st: Foreach looping code for generating a Quantiles variable for each two digit sic code***From:*Nick Cox <njcoxstata@gmail.com>

- Prev by Date:
**Re: st: Foreach looping code for generating a Quantiles variable for each two digit sic code** - Next by Date:
**Re: st: RE: creating loops using combinations of variables** - Previous by thread:
**Re: st: Foreach looping code for generating a Quantiles variable for each two digit sic code** - Next by thread:
**Re: st: Foreach looping code for generating a Quantiles variable for each two digit sic code** - Index(es):