Statalist The Stata Listserver


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

Re: st: Referencing Observations with Nested For Loops


From   Friedrich Huebler <[email protected]>
To   [email protected]
Subject   Re: st: Referencing Observations with Nested For Loops
Date   Mon, 3 Jul 2006 12:16:55 -0700 (PDT)

Alec,

You have to place quotes around the macro names.

gen simscore`x'=games[`x']-years[`i']

Friedrich Huebler

--- "Alec N. Halaby" <[email protected]> wrote:
> 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


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
*
*   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index