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: alternative to forvalues combined with bysort, by


From   Kaspar Dardas <[email protected]>
To   [email protected]
Subject   st: alternative to forvalues combined with bysort, by
Date   Mon, 28 Jun 2010 12:11:22 +0200

Hello,

I would like to run the below forvalues loop for several countries
("locations") in one large dataset. Thus, I simply need to repeat this
loop for subsets in one large dataset in the same fashion as the
bysort: command would provide. "bysort:" , however, cannot be combined
with forvalues.

local N = _N
gen count = .
qui forval i = 1/`N' {
egen tag = tag(transactionid) if gvkey == gvkey[`i'] &
inrange(eventdate, eventdate[`i'] - 365, eventdate[`i'])
count if tag
replace count = r(N) in `i'
drop tag
 }

Therefore, I have tried a foreach loop:

foreach x in location {
	gen count_all_trades = . if location == `x'
	quietly forvalues i = 1/`N' {
		egen tag = tag(transactionid) if location == `x' &
inrange(eventdate, eventdate[`i'] - 365, eventdate[`i'])
		count if tag & location == `x'
		replace count_all_trades = r(N) in `i' if location == `x'
		drop tag
		}
}

However, this does not give me the correct solution. Is there anything
similar to the "bysort:" command which can be used in combination with
the forvalues command?
Again, I simply need to rerun the first command for 8 different
countries in one large dataset. Alliteratively, I could also split my
dataset into 8 subsamples and run the above code 8 times. However, I
would like to find a more "elegant" way to solve this problem.

Best regards,

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