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

st: RE: Scripting Question


From   "David Harrison" <[email protected]>
To   <[email protected]>
Subject   st: RE: Scripting Question
Date   Fri, 29 Apr 2005 16:59:07 +0100

The problem is with the line

foreach k in `i'+1 {

This is setting k equal to the string `i'+1 rather than evaluating it (i.e. when i is 1, k is "1+1" and not 2, as you would want).

In fact, a second foreach is not necessary at all as k only takes one value for each value of i. You can replace the above line with

local k = `i'+1

(and of course remove the close brace for the end of the foreach loop)

Incidentally using -display- to try to check what was going wrong didn't help because when you wrote -display `k'- Stata saw -display 1+1- and evaluated it as 2.

Hope this helps

David

-----Original Message-----
From: Martin M. Monti [mailto:[email protected]]
Sent: 29 April 2005 16:39
To: [email protected]
Subject: st: Scripting Question


Hi Everyone,

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.

 Inpupt will be appreciated!

Cheers,

  Martin


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