Bookmark and Share

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]

st: simplifying foreach loop


From   "Biljana Dlab" <[email protected]>
To   <[email protected]>
Subject   st: simplifying foreach loop
Date   Mon, 11 Oct 2010 21:36:54 +0200

Dear Statalist,

I have firms sorted by SIC from 2000-3999 and would like to compute the
3-digit industry median.

How do I simplify this loop, so that the increment in sic_n are
increasing by 10 without writing out every line?


. foreach v of var r_d {
  2. gen d`v'=.
  3. by sic_n, sort: egen datmedian=median(r_d) if sic_n>=2010 &
sic_n<=2019
  4. by sic_n, sort: egen datmedian=median(r_d) if sic_n>=2020 &
sic_n<=2029
  5. by sic_n, sort: egen datmedian=median(r_d) if sic_n>=2030 &
sic_n<=2039
  6. by sic_n, sort: egen datmedian=median(r_d) if sic_n>=2040 &
sic_n<=2049
  7. by sic_n, sort: egen datmedian=median(r_d) if sic_n>=2050 &
sic_n<=2059
  8. by sic_n, sort: egen datmedian=median(r_d) if sic_n>=2060 &
sic_n<=2069
  9. by sic_n, sort: egen datmedian=median(r_d) if sic_n>=2070 &
sic_n<=2079
(...) 
till
	by sic_n, sort: egen datmedian=median(r_d) if sic_n>3990 &
sic_n<3999
}

Thanks a lot.

Best,
Biljana

*
*   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/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index