Statalist


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

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


From   joe j <[email protected]>
To   [email protected]
Subject   st: forvalues & replace not working under two 'not equal to' conditions
Date   Tue, 10 Nov 2009 16:04:16 +0100

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/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index