Hi statalisters,
I have been working on a short program that doesn't seem to work, I think I'm just missing a small mistake... I have a data file with three columns: citing, cited, nclass. For every "citing", there are multiple "cited", and for each "cited" there is a "nclass". The file is sorted by citing, then nclass. I need a program to count the number of unique "nclass" strings associated to each "citing".
As a simple example, given the following data file "data.dta":
citing cited nclass
100 20 12
100 22 15
100 23 15
101 32 14
101 33 15
101 34 15
101 40 17
I need the following output file:
citing numpatclass
100 2 [12 and 15 are unique, 15 is repeated]
101 3 [14, 15, 17 are unique, 15 is repeated]
I have decided to do it by creating an intermediate file which I will later collapse(max):
citing cited nclass indexpatclass
100 20 12 1
100 22 15 2
100 23 15 2
101 32 14 1
101 33 15 2
101 34 15 2
101 40 17 3
"indexpatclass" indexes by 1 whenever a "citing" involves a new "nclass", and resets to 1 whenever a new "citing" begins. So I have created a short program. It sorts by "citing" and "nclass", then it uses a while-loop, and then two if-loops. But there are two problems: (1) I am getting a "weights not allowed" message when I try to run it. (2) I am also not sure whether I am properly nesting my loops. Can anybody provide any insight? Or alternatively, is there a much simpler way to do what I am attempting?
Thanks, --Chihmao.
--------------------------------
# delimit cr
program define uniqpatclass
use c:\temp\data
generate indexpatclass=0
sort citing nclass
replace indexpatclass=1 in 1
generate id=_n
while id<_N {
if citing[_n]==citing[_n-1] {
if nclass[_n]==nclass[_n-1] {
replace indexpatclass[_n]=indexpatclass[_n-1]
id = `id' + 1
}
else {
replace indexpatclass[_n]=indexpatclass[_n-1]+1
id = `id' + 1
}
}
else {
replace indexpatclass[_n]=1}
id = `id' + 1
}
end
*
* 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/