I agree that you would be better off with the data
-reshape-d to long.
After that one standard protocol is of this form
levelsof id, local(ids)
gen corr = .
qui foreach i of local ids {
ktau rank90 rank05 if id == `i'
replace corr = r(tau_a) if id == `i'
}
After that, each correlation for each individual is necessarily
repeated for each observation for that individual.
You can tag just one observation from each group
using -egen, tag()-.
For more on -foreach- see for example
[P] foreach . . . . . . . . . . . . . . . . . . . . . . . Loop over items
(help foreach)
FAQ . . . . . . . . . . Making foreach go through all values of a variable
8/05 Is there a way to tell Stata to try all values of a
particular variable in a foreach statement without
specifying them?
http://www.stata.com/support/faqs/data/foreach.html
FAQ . . . . . . . . . Counting distinct strings across a set of variables
7/04 How do I count the number of distinct strings
across a set of variables?
http://www.stata.com/support/faqs/data/distinctstrings.html
FAQ . . . . . . . . . . . . . . Getting nice time of day labels on a graph
9/03 How can I get "nice" time of day labels on a graph?
http://www.stata.com/support/faqs/graphics/nicetime.html
Example . . . . Stata learning module: Working across variables using foreach
. . . . . . . . . . . . . . . . . . UCLA Academic Technology Services
8/03 http://www.ats.ucla.edu/stat/stata/modules/acrossvars.htm
SJ-3-2 pr0009 . . . . . . . . . . . . . Speaking Stata: Problems with lists
Q2/03 SJ 3(2):185--202 (no commands)
discusses ways of working through lists held in macros
SJ-2-2 pr0005 . . . . . . Speaking Stata: How to face lists with fortitude
Q2/02 SJ 2(2):202--222 (no commands)
demonstrates the usefulness of for, foreach, forvalues, and
local macros for interactive (non programming) tasks
There are also other versions of the last on the internet, as a Google
will show.
Nick
n.j.cox@durham.ac.uk
Kelvin Foo
> I have a dataset where individuals rank items from 1 to 10 in the
> years 1990 and 2005. I would like to find the Kendall's tau statistic
> for each individual's rankings of items between these two years, and
> store the results in a new variable. In my dataset, the individuals
> are the observations and the rankings are stored in 20 variables,
> named A_90, B_90... J_90, A_05, B_05,.. J_05. A to J are the 10 items
> and '90', '05' are the years.
>
> How can I carry out this task?
>
> My guess is to first use -reshape- to have 1990's rankings stored in
> one variable (rank90), and 2005's rankings in another variable
> (rank05). Each individual would have an identifier number associated
> with him, and this will appear 10 times in the reshaped long format.
>
> Next, I was thinking of running
>
> generate correl=. // variable for storing Kendall tau results
> by identifier: ktau rank90 rank05
>
> But how do I get Stata to store the r(tau_a) result in correl for each
> individual before moving on to the next one?
>
> Or is there an alternative way in which I can find the Kendall tau for
> each observation, given that my rankings are stored across different
> variables?
*
* 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/