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]

Re: st: AW: alternative to forvalues combined with bysort, by


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

Hi Martin,

thanks for all the help and effort. You definitely do not have to be
sorry. Its me who doesn't understand this stuff. Well, I am still
struggling with it. I think, however, I know where the problem is but
my Stata knowledge is just too bad to solve it. By the way, your
solution "(forv i=1/`=_N'{" gets the exact same results as "quietly
forvalues i = 1/`N' {"

My actual problem is that I want to tag all "transactionsids" in ONE
country 365 days prior to the current transaction. There are about
80000 transactions. In each country 10000.

--> egen tag = tag(transactionid) & inrange(eventdate, eventdate[`i']
- 365, eventdate[`i'])

All of this works fine for one country (whenever I drop the other 7
countries). However, whenever I try to use double loop version I still
get the results for my entire datasample. Thus, (very simply speaking)
all eight countries are treated as one. I think this has something do
with

local N = _N
qui forval i = 1/`N' { (or your version forv i=1/`=_N)

the _N refers to the entire dataset but I need somehow to apply the _N
for each country separately so not for 80000 but rather for 8*10000
observations. Stata also does not allow to use

bysort location: local N = _N

The easiest way would be to drop the remaining seven countries and do
all this eight times. However, I would like to find a better solution.
Maybe you have one last advice on this for me. Thanks for all of your
help again!


Best,

Kaspar

2010/6/28 Martin Weiss <[email protected]>:
>
> <>
>
> Re your assertion that -bysort- and -forvalues- do not work together: This has never been a problem for me, since -bysort- is so incredibly useful and versatile. For an intro, see NJC`s http://www.stata-journal.com/sjpdf.html?articlenum=pr0004
>
>
>
> HTH
> Martin
>
>
> -----Ursprüngliche Nachricht-----
> Von: [email protected] [mailto:[email protected]] Im Auftrag von Martin Weiss
> Gesendet: Montag, 28. Juni 2010 13:19
> An: [email protected]
> Betreff: AW: st: AW: alternative to forvalues combined with bysort, by
>
>
> <>
>
> Sorry about my mistake, I never noticed you had a loop nested in there, b/c of the -quietly- command in front of it. One of the problems in your code is
>
> *************
> quietly forvalues i = 1/`N' {
> *************
>
> which should be - forv i=1/`=_N'{-
>
>
> HTH
> Martin
>
> -----Ursprüngliche Nachricht-----
> Von: [email protected] [mailto:[email protected]] Im Auftrag von Kaspar Dardas
> Gesendet: Montag, 28. Juni 2010 12:30
> An: [email protected]
> Betreff: Re: st: AW: alternative to forvalues combined with bysort, by
>
> Martin,
>
> thanks for the prompt reply. Nesting two forvalues loops in a similar
> fashion as my second solution which includes "foreach" ? I think you
> are referring to something that I have already tried and showed in my
> firs email, correct? Or did you mean something else? I think my first
> email might have been not very precise. Again, my first command in the
> below email should run over 8 countries. My second command is my
> (failed) approach to solve it (which I think is similar to nesting two
> forvalues loops)
>
> Best,
>
> Kaspar
>
> 2010/6/28 Martin Weiss <[email protected]>:
>>
>> <>
>>
>>
>> You may want to nest two -forvalues- loops...
>>
>>
>>
>> HTH
>> Martin
>>
>>
>> -----Ursprüngliche Nachricht-----
>> Von: [email protected] [mailto:[email protected]] Im Auftrag von Kaspar Dardas
>> Gesendet: Montag, 28. Juni 2010 12:11
>> An: [email protected]
>> Betreff: st: alternative to forvalues combined with bysort, by
>>
>> 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/
>>
>>
>> *
>> *   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/
>
>
> *
> *   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/
>

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