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

st: RE: Scripting Question


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: Scripting Question
Date   Fri, 29 Apr 2005 17:06:10 +0100

Assuming you want 

1 with 2 
3 with 4
5 with 6 
7 with 8 

then it is one loop, not two, and 
better phrased that way: 

forval i = 1(2)7 {
	local k = `i' + 1
	... 
}

What you have is almost legal. You'd need 

forval i = 1(2)7 {
	foreach k in `=`i'+1' {
		... 
	}
}

but as the inner loop is over 
a single index, you can replace it as above. 

Nick 
n.j.cox@durham.ac.uk 

Martin M. Monti
 
> I'm trying to write a .do file that may scroll through my variables 
> performing simple tabstats and ttests on pairs of variables. The key 
> variables to test/tabstat are identified by a string (e..g 
> "judged") and an 
> appended number (thus: e..g judged1, judged2, ...). A simple 
> loop on an `i' 
> macro will allow me to specify judged`i' in the loop and have 
> it scroll 
> through the various variables, but when I set a second macro 
> (say `k') 
> making it equal to `i'+1 (so that I can always do ttestes on 
> variables in 
> sequence (e.g. ttest time`i' = time`k' so that it's always 
> "ttest time1 = 
> time2", "ttest time3 = time4", and so on)) even though it 
> will loop through 
> the correct numbers (thus `k' is correctly being set to 
> `i'+1), it will not 
> allow me to append it to the string (unlike `i'!).
> 
> Here is the loop I use:
> 
> *the loop only sets `i' to odd numbers (so that `k' can 
> scroll through the 
> even ones)
> forvalues i = 1 (2) 7 {
>    foreach k in `i'+1 {
>        table judged`i' judged`k'
>        tabstat time`i' time`k', stats(mean sd N)
>        ttest time`i' = time`k' if (judged`i' == 1 & judged`k' == 1)
>        tabstat rate`i' rate`k', stats(mean sd N)
>        ttest rate`i' = rate`k' if (judged`i' == 1 & judged`k' == 1)
>    }
> }
> 
> Note, I''ve tried running the loop and the problem is that as 
> it is supposed 
> to execute the commands with `k' it will say that the 
> variable judged (only 
> with the `k' appended!) is ambiguous. Also, if I run the same 
> exact loop, 
> just replacing the 5 commands with only: display `i' and 
> display `k' it will 
> scroll correctly through the numbers.
 

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