Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: data manipulation - generating pairs of a list


From   Ichiro Fujikake <fujikake@cc.miyazaki-u.ac.jp>
To   <statalist@hsphsun2.harvard.edu>
Subject   Re: st: data manipulation - generating pairs of a list
Date   Fri, 20 Aug 2004 15:10:33 +0900

There must be smarter ways to do this, but the one I came up is in the
following do file.

===========================
clear
set obs 4

gen country1="Paraguay"
replace country1="Italy" if _n==2
replace country1="Ghana" if _n==3
replace country1="Japan" if _n==4

tempvar id1 id2
gen `id1'=_n

local n=_N
forv i=1/`n' {
    tempvar c`i'
    gen `c`i''=country1[`i']
}

drop in `n'
expand `n'-_n
bysort `id1' : gen `id2'=`id1'+_n
gen country2=""

forv i=2/`n' {
    replace country2= `c`i'' if `id2'==`i'
}

exit
===========================


Ichiro Fujikake


On 04.8.20 11:29, "Mark Manger" <manger@interchange.ubc.ca> wrote:

> Hi,
> 
> I'm struggling with a data manipulation problem: I have a dataset with list
> of observations on a string variable and want to create pairwise
> combinations. My dataset looks like this:
> 
> country1
> A
> B
> C
> 
> I need the possible combinations so that the new dataset looks like
> 
> country1 country2
> A B
> A C
> C B
> 
> I could take the same list in two different datasets and then use the
> command "cross", but that gives me 3! combinations:
> 
> AB AC BA BC CA CB
> 
> For my purposes, AB and BA are identical. How do I create the list?
> 
> Cheers,
> 
> Mark
> 
> 
> 
> Mark Manger
> 
> University of British Columbia
> manger@interchange.ubc.ca
> 
> *
> *   For searches and help try:
> *   http://www.stata.com/support/faqs/res/findit.html
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/
> 

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



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