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: foreach or forvalues to re-structure data


From   Joe Howard <[email protected]>
To   statalist <[email protected]>
Subject   Re: st: foreach or forvalues to re-structure data
Date   Tue, 12 Nov 2013 18:04:40 -0500

Try using collapse instead -- it's more efficient...

gen minid=3Dmin(country, partner)
gen maxid=3Dmax(country, partner)
drop country partner
collapse (sum) trade, by(minid maxid)

On Tue, Nov 12, 2013 at 5:24 PM, Saleheen, Jumana
<[email protected]> wrote:
> Hi,
>
> I’d like to use the commands foreach or forvalues to re-structure my dataset. Happy to use other commands if they work better.
>
> What I have is a dataset that  looks like this:  country 1 trades with country 2 and 3, but not with itself
>
> country       partner           trade
> 1              1              0
> 1              2              20
> 1              3              30
> 2              1              10
> 2              2              0
> 2              3              30
> 3              1              20
> 3              2              10
> 3              3              0
>
> And I want to transform the data to look like this:
>
> Rowcol  sumvar
> 12,21    30
> 13,31    50
> 23,32    40
>
> What I want is the pair-wise trading between two countries.
>
> So, in the end the number of observations I have should be much smaller, and equal to the pairs of countries in my data set.  In the above example, it would be 3.
>
> Thanks
> Jumana
>
> ***********************************************************************************
> This e-mail is intended for the addressee(s) named above and any other use is prohibited. It may contain confidential information. If you  received this e-mail in error please contact the sender by return e-mail.
>
> The Bank of England does not accept legal responsibility for the contents of this message if it has reached you via the Internet, as Internet communications are not secure. Any opinions expressed are those of the author and are not necessarily endorsed by the Bank of England.
>
> Recipients are advised to apply their own virus checks to this message .
> ***********************************************************************************
>
> _____________________________________________________________________
> This e-mail has been scanned for known viruses by the Messagelabs SkyScan Service.
>
> *
> *   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/



-- 
_______________________________________________
Joe Howard                                                     [in] LinkedIn
Doctoral Student
Center for the Study of Higher and Postsecondary Education
University of Michigan

*
*   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