Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Invalid name error in foreach loop

    Hi, I'm having trouble in running a foreach loop and it keeps showing the invalid name error to me. The commands I used are as following:

    global NEWIVB "rfriend hfriend trelat rrelat hrelat"

    foreach newivb of global `NEWIVB' {
    ta `newivb'
    ta `newivb', nolabel
    recode `newivb' (1 = 3) (3 = 1) (4 = 0), gen(`newivb'_new)
    ta `newivb'_new
    }

    And the error is like this below:
    { invalid name
    r(198);

    I'm not quiet sure if I type something wrong here and I'm wondering if anyone could give me a hint on that? Thank you so much!

  • #2
    Take those `' quotes off of NEWIVB (but not from newivb). You defined NEWIVB to be a -global- macro: you do not dereference global macros with `' quotes, you use the $ for that. But, in any case, what appears after -foreach whatever of global- must be the name of the global macro, so no dereferencing is appropriate here.

    Comment


    • #3
      Originally posted by Clyde Schechter View Post
      Take those `' quotes off of NEWIVB (but not from newivb). You defined NEWIVB to be a -global- macro: you do not dereference global macros with `' quotes, you use the $ for that. But, in any case, what appears after -foreach whatever of global- must be the name of the global macro, so no dereferencing is appropriate here.
      Thank you so much for the help and clarification Clyde! It works after I get rid of the quotes!

      Comment

      Working...
      X