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

st: Re: data manipulation


From   Christopher F Baum <baum@bc.edu>
To   statalist@hsphsun2.harvard.edu
Subject   st: Re: data manipulation
Date   Fri, 20 Aug 2004 08:12:46 -0400

On Aug 20, 2004, at 2:33 AM, Mark 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

Note that if we take the last item to be B C then your problem is that of generating the elements in the lower triangle (strictly, subdiagonal, since you don't want A A) of a symmetric matrix. That sort of thing is easily done with nested loops, where the inner loop's lower index is driven by the upper loop's current index:

set obs 325
g var1 = ""
g var2 = ""
local k 0
local list1 `c(alpha)'
local wc : word count `list1'
forvalues i=1/`wc' {
local f : word `i' of `list1'
forvalues j=`++i'/`wc' {
local s : word `j' of `list1'
local list2 "`list2' `f'`s'"
local ++k
qui {
replace var1 = "`f'" in `k'/`k'
replace var2 = "`s'" in `k'/`k'
}
}
}
di "`list2'"
l var1 var2


Kit

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