Bookmark and Share

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]

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


From   "Martin Weiss" <martin.weiss1@gmx.de>
To   <statalist@hsphsun2.harvard.edu>
Subject   AW: st: AW: alternative to forvalues combined with bysort, by
Date   Mon, 28 Jun 2010 13:18:40 +0200

<> 

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: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] Im Auftrag von Kaspar Dardas
Gesendet: Montag, 28. Juni 2010 12:30
An: statalist@hsphsun2.harvard.edu
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 <martin.weiss1@gmx.de>:
>
> <>
>
>
> You may want to nest two -forvalues- loops...
>
>
>
> HTH
> Martin
>
>
> -----Ursprüngliche Nachricht-----
> Von: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] Im Auftrag von Kaspar Dardas
> Gesendet: Montag, 28. Juni 2010 12:11
> An: statalist@hsphsun2.harvard.edu
> 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/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index