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]
RE: st: RE: replace values through observations with criterion
From
Joe Canner <[email protected]>
To
"[email protected]" <[email protected]>
Subject
RE: st: RE: replace values through observations with criterion
Date
Wed, 16 Oct 2013 01:14:50 +0000
Sorry, yes, I missed something. Assuming that my idea was helpful (still not clear from the explanation), you need to add a statement that can populate the TargetTariff variable to all values of the by-group. For example:
. bysort Product Partner Year: gen TargetTariff=Tariff if Reporter=="UK"
. bysort Product Partner Year: egen TT=min(TargetTariff)
. bysort Product Partner Year: replace Tariff=TT if Reporter=="GERMANY"
or
. bysort Product Partner Year: gen TargetTariff=Tariff if Reporter=="UK"
. bysort Product Partner Year TargetTariff: replace TargetTariff=TargetTariff[_n-1] if mi(TargetTariff)
. bysort Product Partner Year: replace Tariff=TT if Reporter=="GERMANY"
or (not sure if this will work)
. bysort Product Partner Year: gen Target_n=_n if Reporter=="UK"
. bysort Product Partner Year: egen TT_n=min(Target_n)
. bysort Product Partner Year: replace Tariff=Tariff[TT_n] if Reporter=="GERMANY"
________________________________________
From: [email protected] [[email protected]] on behalf of Abssi, Fouad [[email protected]]
Sent: Tuesday, October 15, 2013 9:00 PM
To: [email protected]
Subject: RE: st: RE: replace values through observations with criterion
Dear Nick and Joe,
Thank you for your responds.
I meant with criterion: the combination of Product, Partner, and Year is the same.
For example, for the first observation (were the Reporter = Germany) from the data i provided: I want from stata to look for the observation were the UK is reporter and the combination of Product, Partner, and Year is the same(here, Product =111, Partner= Australia, Year=1992). Then, replace the tariff in the first observation with the tariff in the observation ( Reporter = UK, Product =111, Partner= Australia, Year=1992). I want to do this for about 25 countries and very big number of combinations of Product, Partner, and Year.
Actually, I tried the code that Joe suggested, and it generates missing values were the reporter is not Uk. So, replace will not work here.
Regards,
Fouad
________________________________________
From: [email protected] [[email protected]] on behalf of Nick Cox [[email protected]]
Sent: 16 October 2013 01:45
To: [email protected]
Subject: Re: st: RE: replace values through observations with criterion
No, as said, that does nothing useful. You are missing a line
spreading the -TargetTariff- variable within groups of observations.
But, as we agreed earlier, it is hard to imagine that this is quite
what was asked for. (A fix from "Germany" to "GERMANY" is a typo fix.)
. input str9 Reporter Product str9 Partner Year Tariff
Reporter Product Partner Year Tariff
1. GERMANY 111 Australia 1992 .
2. UK 111 Australia 1992 3.2
3. USA 111 Australia 1992 0
4. end
. bysort Product Partner Year: gen TargetTariff=Tariff if Reporter=="UK"
(2 missing values generated)
. bysort Product Partner Year (TargetTariff) : replace TargetTariff =
TargetTariff[1]
(2 real changes made)
. bysort Product Partner Year: replace Tariff=TargetTariff if
Reporter=="GERMANY"
(1 real change made)
Nick
[email protected]
On 16 October 2013 01:26, Joe Canner <[email protected]> wrote:
> Unfortunately, I don't have the ability to test the code at the moment, but let me illustrate what I think the code is going to do.
>
> Picture the data sorted by Product, Partner and Year:
> Reporter Product Partner Year Tariff
> GERMANY 111 Australia 1992 .
> UK 111 Australia 1992 3.2
> USA 111 Australia 1992 0
> etc.
>
> . bysort Product Partner Year: gen TargetTariff=Tariff if Reporter=="UK"
>
> will generate a new variable, TargetTariff, that has the value of 3.2 (UK's value) for each observation in the by group:
>
> Reporter Product Partner Year Tariff TargetTariff
> GERMANY 111 Australia 1992 . 3.2
> UK 111 Australia 1992 3.2 3.2
> USA 111 Australia 1992 0 3.2
> etc.
>
> . bysort Product Partner Year: replace Tariff=TargetTariff if Reporter=="Germany"
>
> will replace the Tariff for Germany with the TargetTariff (previously defined as the UK value):
> Reporter Product Partner Year Tariff TargetTariff
> GERMANY 111 Australia 1992 3.2 3.2
> UK 111 Australia 1992 3.2 3.2
> USA 111 Australia 1992 0 3.2
> etc.
>
> If Fouad needs to do any other swaps, he can repeat the process and then drop TargetTariff at the end. Am I missing something? Perhaps there is a better way to do it, but absent a better defined problem, this seemed to be a reasonable approach.
>
> Joe
>
> ________________________________________
> From: [email protected] [[email protected]] on behalf of Nick Cox [[email protected]]
> Sent: Tuesday, October 15, 2013 8:01 PM
> To: [email protected]
> Subject: Re: st: RE: replace values through observations with criterion
>
> We agree in not understanding what is desired here, but I don't
> understand your suggestion.
>
> The -by:- prefix here will not affect the operation of the code.
>
> Moreover, as -Reporter- cannot simultaneously be "UK" and "Germany", I
> don't believe this code will do anything to change existing values.
>
> Nick
> [email protected]
>
>
> On 16 October 2013 00:53, Joe Canner <[email protected]> wrote:
>> Fouad,
>>
>> It sounds like what you want is this (at least for the example you provided):
>>
>> . bysort Product Partner Year: gen TargetTariff=Tariff if Reporter=="UK"
>> . bysort Product Partner Year: replace Tariff=TargetTariff if Reporter=="Germany"
>>
>> Note, however, that this will replace the Germany tariff with the UK tariff for *every* combination of Product, Partner, and Year, and it is not clear that this is what you are asking for. Note also that you will have to repeat this process for every pair of Reporters that you want to swap. As Nick pointed out, if you have a particular criterion in mind for making this replacement, it would be helpful to know that so as to make the code more generalizable.
>>
>> Regards,
>> Joe Canner
>> Johns Hopkins University School of Medicine
>> ________________________________________
>> From: [email protected] [[email protected]] on behalf of Abssi, Fouad [[email protected]]
>> Sent: Tuesday, October 15, 2013 7:12 PM
>> To: [email protected]
>> Subject: st: replace values through observations with criterion
>>
>> I would like to copy a value of a variable in an observation and replace it to another observation in the same variable with criterion. I am using Stata 12.1 for Windows. I have 1754298 observations, and five variables: reporter, product, year, exporter, and tariff ON tariff data. My data looks like this.
>>
>> Reporter Product Partner Year Tariff
>> GERMANY 111 Australia 1992 .
>> GERMANY 111 Australia 1993 4.36
>> GERMANY 111 Canada 1995 7.01
>> GERMANY 123 Canada 1992 0
>> GERMANY 123 Canada 1995 5
>> GERMANY 251 Chile 1993 2.81
>> UK 111 Australia 1992 3.2
>> UK 111 Australia 1993 3.75
>> UK 111 Canada 1995 7
>> UK 123 Canada 1992 5.94
>> UK 123 Canada 1995 5.48
>> UK 251 Chile 1993 4.29
>> USA 111 Australia 1992 0
>> USA 111 Australia 1993 4
>> USA 111 Canada 1995 5
>> USA 123 Canada 1992 2.73
>> USA 123 Canada 1995 3.62
>> USA 251 Chile 1993 4.39
>>
>> I would like to tell Stata to copy tariff value from the observation where UK is Reporter (for example) to the observation where GERMANY is Reporter (for example), and (Product, year, and Partner are the same). Please note that the number of observations is 1754298, and for Reporter and Partner there are 50 countries, for Product there are almost 26 product, for Year there is 18 years.
>>
>> Many Thanks,
>> Fouad Abssi
>>
>> *
>> * For searches and help try:
>> * http://www.stata.com/help.cgi?search
>> * http://www.stata.com/support/faqs/resources/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/faqs/resources/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/faqs/resources/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/faqs/resources/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/faqs/resources/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/faqs/resources/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/faqs/resources/statalist-faq/
* http://www.ats.ucla.edu/stat/stata/