Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: Referencing Observations with Nested For Loops


From   "Alec N. Halaby" <halaby@fas.harvard.edu>
To   statalist@hsphsun2.harvard.edu
Subject   st: Referencing Observations with Nested For Loops
Date   Mon, 03 Jul 2006 15:00:44 -0400

Dear Stata Community,

I'm trying to write a straightforward program that is built around nested "forvalues" loops, and I've encountered difficulties in referencing specific observations using the counters. I've setup the for loops as follows, with the letters x and i used for the counters.:
forvalues x=1/4539 {
if d04_05==1 & year==2004 {
forvalues i=1/4539 {
gen simscore`x'=games[x]-years[i]......

I would like to be able to retrieve data from two different observations in generating a new variable; for instance, observation 117 and observation 2,337 to generate games[117]-years[2337]. The reference and STATALIST material I've read has suggested that I use explicit subscripting to do this, and my instinct is to use square brackets to select an observation (i.e. games[117]-years[2,337]). However, when I insert the counters into the square brackets (games[x]-years[i], or in plain english "the games for observation x minus the years for observation i"), I get the error message that Stata is interpreting the contents of the brackets as weights. How would I use the counters to reference 2 different observations? I'm relatively new to STATA, so the answer may be as simple as a basic indexing method. Thanks in advance for any help you can provide!

Best,

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