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

From |
"Martin M. Monti" <mmonti@Princeton.EDU> |

To |
<statalist@hsphsun2.harvard.edu> |

Subject |
st: Re: RE: Scripting Question |

Date |
Fri, 29 Apr 2005 13:17:26 -0400 |

Nick, David, Nicholas,

The "local" works much better, I actually didn't know about it, being this my very first scripting. But at least it's all clear now!

Thank you very much for the help.

Best,

Martin

----- Original Message ----- From: "Nick Cox" <n.j.cox@durham.ac.uk>

To: <statalist@hsphsun2.harvard.edu>

Sent: Friday, April 29, 2005 12:06 PM

Subject: st: RE: Scripting Question

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. MontiI'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/

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

**References**:**st: RE: Scripting Question***From:*"Nick Cox" <n.j.cox@durham.ac.uk>

- Prev by Date:
**st: Follow up on matrix building question -- correct version** - Next by Date:
**st: RE: Follow up on matrix building question -- correct version** - Previous by thread:
**st: RE: Scripting Question** - Next by thread:
**st: Follow up on matrix building question** - Index(es):

© Copyright 1996–2014 StataCorp LP | Terms of use | Privacy | Contact us | What's new | Site index |