Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: st: RE: looping up to a local macro

From   "Sarah Edgington" <>
To   <>
Subject   RE: st: RE: looping up to a local macro
Date   Thu, 23 Jun 2011 10:48:50 -0700

It sounds like maybe there's a lot of code in the loop that you aren't
showing us.  One thing that might be helpful is to -set trace on- right
before the chunk of code that's causing problems.  That way you'll be able
to see exactly what line of code is tripping Stata up.  

Do you get the invalid syntax message even if you run the loop without the
macro?   For example, -forvalues i=1(1)50- ?  If you do then the problem is
with something else in the code and my advice above should help you pinpoint
exactly what the issue is.  If specifying a number instead of the local
macro letters solves the problem, that suggests that Stata doesn't recognize
the macro.  Things you might check in that case are whether you've made a
mistake typing the name of the macro and whether you're setting the macro in
the same dofile you're calling it from.  If you're testing things
interactively in Stata you'll need to set the macro before running the loop
even if you've done it previously in a dofile; Stata "forgets" all the local
macros as soon as a dofile completes.


-----Original Message-----
[] On Behalf Of Michael Costello
Sent: Thursday, June 23, 2011 7:38 AM
Subject: Re: st: RE: looping up to a local macro

Wow, this became a much bigger thing than I was anticipating!  Thanks for
all the interest though.  I guess I'm wondering if I am thinking of the
local macro correctly, in that it's a kind of proxy for some value (in my
case 50, 100, etc) which can have operations done unto it, or if it's
something else that I don't understand.

I manage ~30 databases.  Most of them need the same bits of code run on
them, but each has a different number of variables upon which that code must
be run.  For example:

Database 1 has variables letter1, letter2, letter3, . . . , letter100
Database 2 has variables letter1, letter2, letter3, . . . , letter50

I would like to create this "master cleaning file" which, after establishing
a local macro value of either 50 or 100, would do the

**Section: Letter Names**
egen letter_score2=rowtotal(letter1-letter`letters'), missing egen
gen letter_score_pcnt=letter_score/`letters'
gen letter_score_zero= (letter_score==0) if letter_score<.
gen letter_attempted_pcnt=letter_score/letter_attempted
gen clpm=round(letter_score/(1-(letter_time_remain/60)))

But the error I'm getting is: "letter_sound ambiguous abbreviation"
for the first line of the code

Sarah, the error i'm getting when I try to run a loop is "invalid syntax"
forvalues i = 1(1)`letters' {
   quietly: rename letter0`i' letter`i'
   quietly: recode letter`i' (9=.) (2=1) (1=0)
   * more lines of code here
invalid syntax

Michael Costello

"To call in the statistician after the experiment is done may be no more
than asking him to perform a post-mortem examination: he may be able to say
what the experiment died of."  -Sir Ronald Aylmer Fisher, FRS

*   For searches and help try:

*   For searches and help try:

© Copyright 1996–2017 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index