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]

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


From   "Martin Weiss" <[email protected]>
To   <[email protected]>
Subject   AW: st: AW: alternative to forvalues combined with bysort, by
Date   Mon, 28 Jun 2010 13:25:40 +0200

<> 

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/


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