[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 To statalist@hsphsun2.harvard.edu Subject Re: st: AW: forvalues & replace not working under two 'not equal to' conditions Date Tue, 10 Nov 2009 17:38:52 +0100

```Thanks Martin. I think I wasn't clear enough in the last mail. I was
not looking at various combinations of firm_id, nation_id and
contract_id 'for each observation'. Rather I was looking at the
similarity or difference of firm_id/nation_id 'between two or more
observations' under each contract_id.

Based on Martin's suggestion I could derive group_d (see below). But I
still can't get right nongroup_f, which equals 1 (for all
observations) if firm_id and nation_id are different for two or more
observations under each contract_id (but it takes a value 1, wrongly,
for all observations in the data)

*deriving group_d (this works)
egen groups=group(firm_id nation_id)

bys contract_id (groups):  /*
*/ gen byte distinctcount_group_d= /*
*/ (groups[_n]==groups[_n+1])

bys contract_id (groups):  /*
*/ replace distinctcount_group_d=1 /*
*/ if (groups[_n]==groups[_n-1])

*2 deriving nongroup_f doesnt work (e.g. it should be 0 for contract_id=1)
bys contract_id (groups):  /*
*/ gen byte distinctcount_nongroup_f= /*
*/ (groups[_n]~=groups[_n+1]) & (nation_id[_n]~=nation_id[_n+1])

bys contract_id (groups):  /*
*/ replace distinctcount_nongroup_f=1 /*
*/ if (groups[_n]~=groups[_n-1]) & (nation_id[_n]~=nation_id[_n-1])

On Tue, Nov 10, 2009 at 4:14 PM, Martin Weiss <martin.weiss1@gmx.de> wrote:
>
> <>
>
> I think a variable denoting the combinations between the three ids is a good
> place to start for you:
>
>
>
> *************
> 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
>
> egen groups=group(contract_id firm_id nation_id)
>
> l, sepby(con) noobs
> *************
>
>
>
> HTH
> Martin
>
>
> -----Ursprüngliche Nachricht-----
> Von: owner-statalist@hsphsun2.harvard.edu
> [mailto:owner-statalist@hsphsun2.harvard.edu] Im Auftrag von joe j
> Gesendet: Dienstag, 10. November 2009 16:04
> An: statalist@hsphsun2.harvard.edu
> Betreff: st: forvalues & replace not working under two 'not equal to'
> conditions
>
> My dataset has three variables 1. contract_id, 2. firm_id and 3.
> nation_id. I want to create 4 variables, each of which gets a value of
> 1 if certain conditions are met. The variables I want to create are
> specific to the contract id, and are:
>
> 1. group_d = 1 when both firm_id and nation_id are same for two or
> more firms with the same contract id
> 2. group_f = 1  when firm_id is same but nation_id is different for
> two or more firms with the same contract id
> 3. nongroup_d = 1  when firm_id is different but nation_id is same for
> two or more firms with the same contract id
> 4 .nongroup_f = 1  when both firm_id and nation_id are different for
> two or more firms with the same contract id
>
> The following code works well for the first three variables, but not
> for the last, nongroup_f; the value is 1 for all observations. I can't
> figure out why.
>
> This is a sample code:
>
> clear
> inp str10(contract_id firm_id   nation_id)
> 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
>
>
> *1.group_d . WORKS!
> gen group_d=.
> forvalues i=1/`=_N'{
> bys contract_id: replace group_d=1 if firm_id==firm_id[_n-`i'] &
> nation_id==nation_id[_n-`i']
> }
> forvalues i=1/`=_N'{
> bys contract_id: replace group_d=1 if firm_id==firm_id[_n+`i'] &
> nation_id==nation_id[_n+`i']
> }
>
> *2.group_f  WORKS!
> gen group_f=.
> forvalues i=1/`=_N'{
> bys contract_id: replace group_f=1 if firm_id==firm_id[_n-`i'] &
> nation_id!=nation_id[_n-`i']
> }
> forvalues i=1/`=_N'{
> bys contract_id: replace group_f=1 if firm_id==firm_id[_n+`i'] &
> nation_id!=nation_id[_n+`i']
> }
>
> *3. nongroup_d  WORKS!
> gen nongroup_d=.
> forvalues i=1/`=_N'{
> bys contract_id: replace nongroup_d=1 if firm_id!=firm_id[_n-`i'] &
> nation_id==nation_id[_n-`i']
> }
> forvalues i=1/`=_N'{
> bys contract_id: replace nongroup_d=1 if firm_id!=firm_id[_n+`i'] &
> nation_id==nation_id[_n+`i']
> }
>
> *4.nongroup_f DOESN'T WORK!!
> 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'])
> }
> *
> *   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–2017 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index