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 LP <statalist@hsphsun2.harvard.edu> |

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

Date |
Thu, 16 Feb 2012 15:14:54 -0600 |

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/

**Follow-Ups**:**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: RE: RE: IPUMS data open issue** - Next by Date:
**Re: st: RE: RE: IPUMS data open issue** - Previous by thread:
**st: Statalist commandments** - Next by thread:
**Re: st: Foreach looping code for generating a Quantiles variable for each two digit sic code** - Index(es):