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]

Re: st: RE: replace values through observations with criterion


From   Nick Cox <njcoxstata@gmail.com>
To   "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>
Subject   Re: st: RE: replace values through observations with criterion
Date   Wed, 16 Oct 2013 01:45:43 +0100

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
njcoxstata@gmail.com


On 16 October 2013 01:26, Joe Canner <jcanner1@jhmi.edu> 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: owner-statalist@hsphsun2.harvard.edu [owner-statalist@hsphsun2.harvard.edu] on behalf of Nick Cox [njcoxstata@gmail.com]
> Sent: Tuesday, October 15, 2013 8:01 PM
> To: statalist@hsphsun2.harvard.edu
> 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
> njcoxstata@gmail.com
>
>
> On 16 October 2013 00:53, Joe Canner <jcanner1@jhmi.edu> 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: owner-statalist@hsphsun2.harvard.edu [owner-statalist@hsphsun2.harvard.edu] on behalf of Abssi, Fouad [fabssi@essex.ac.uk]
>> Sent: Tuesday, October 15, 2013 7:12 PM
>> To: statalist@hsphsun2.harvard.edu
>> 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/


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