Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: AW: forvalues & replace not working under two 'not equal to' conditions


From   joe j <joe.stata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: AW: forvalues & replace not working under two 'not equal to' conditions
Date   Wed, 11 Nov 2009 00:05:41 +0100

Thanks Martin and Nick for your time and insights. I understood that
Martin was patiently showing me better ways, and I am thankful that he
showed me the way fully in his last mail. I also appreciate Nick's
explanations about the loop (I had tried different maximum value for
`i' including 1, 2 etc, but the results remained the same). Thanks so
much again.

On Tue, Nov 10, 2009 at 10:51 PM, Nick Cox <n.j.cox@durham.ac.uk> wrote:
> I've not been following this closely. It seemed to me that Martin was pointing you towards better ways to work but that you were reluctant to abandon your own code.
>
> Whether that's right or wrong, look at your second -forval- loop, for example.
>
> 1. Your loop ends when the index i is the number of observations _N and the subscript firm_id[_n + `i'] is thus firm_id[_n +  _N].
>
> 2. That subscript is _always_ outside the range of observations, as it varies from 1 + _N to _N + _N.
>
> 3. Thus you are referring to a value of -firm_id- that doesn't exist, but that's not illegal and Stata substitutes the empty string "".
>
> 4. It's always going to be true that this is _not_ equal to any non-empty string.
>
> 5. Your code does all this within -by contract_id:- but that does not change the fundamentals. Outside the range means outside the current -by:- group and the problem still bites.
>
> 6. Actually your code also mixes the overall number of observations _N -- which is fed to it from the -forval- loop -- and the within-group observation number _n, which is used by virtue of -by:-. That's not illegal, but it doesn't seems to be what you want at all.
>
> Nick
> n.j.cox@durham.ac.uk
>
> joe j
>
> Thanks again Martin. With your approach one could get the same results
> as with my original codes (in the original post) for the first three
> variables. My problem is with the last variable. I don't understand
> why the following code does not work (which aims variable 'nongroup_f'
> to take the value 1 only when 'firm_id' as well as 'nation_id' of
> preceding and/or following observations are different ). Instead the
> variable takes the value 1 for all observations:
>
> gen nongroup_f=.
>
> forvalues i=1/`=_N'{
> bys contract_id: replace nongroup_f=1 if (firm_id~=firm_id[_n-`i']) &
> (nation_id~=nation_id[_n-`i'])
> }
>
> forvalues i=1/`=_N'{
> bys contract_id: replace nongroup_f=1 if (firm_id~=firm_id[_n+`i']) &
> (nation_id~=nation_id[_n+`i'])
> }
>
> On Tue, Nov 10, 2009 at 6:25 PM, Martin Weiss <martin.weiss1@gmx.de> wrote:
>>
>> <>
>>
>> So a revised version for your first dummy is:
>>
>>
>> *************
>> clear*
>> inp byte(contract_id firm_id)  /*
>> */ nation_id:mylabel, auto
>> 1   2   "US"
>> 1   2   "US"
>> 4   3   "UK"
>> 4   3   "US"
>> 8   4   "US"
>> 8   4   "UK"
>> 8   3   "US"
>> 9   5   "US"
>> 9   4   "UK"
>> 9   3   "US"
>> 10   5   "US"
>> 10   5   "US"
>> 10   6   "NL"
>> 10   7   "NL"
>> 10   4   "UK"
>> 10   4   "CH"
>> end
>>
>> bys contract_id firm_id nation_id:  /*
>> */ egen mycount=count(nation_id)
>>
>> gen byte group_d=mycount>1 & !mi(mycount)
>> drop mycount
>>
>> l, sepby(contract_id) noo
>>
>> *************
>
> *
> *   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   |   What's new   |   Site index