Announcement

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

  • Invalid syntax error when combining foreach and logout ttable2

    Hi,

    I am trying to output the ttable2 results of different groups. The code I am writing is as follows:

    Code:
    foreach group of grouplist new1 sand sean female highschool{
    logout, save (E:\Data\stata\des_new\`group') excel replace: ttable2 weight long education numest perc, by(`group')
    }
    But the stata keeps saying "invalid syntax".

    I have no problem to run:

    Code:
    logout, save (E:\Data\stata\des_new\new1) excel replace: ttable2 weight long education  numest perc, by(new1)
    I will appreciate it very much if someone can tell me what is wrong here. Thanks!

  • #2
    Dara:
    what if you replace
    Code:
    by(`group')
    with

    Code:
    by(group)
    Kind regards,
    Carlo
    (Stata 18.0 SE)

    Comment


    • #3
      grouplist is quite illegal here.

      After

      Code:
      of 
      only certain reserved words are allowed in foreach statements, namely one of the following forms:

      foreach lname of local lmacname {

      foreach lname of global gmacname {

      foreach lname of varlist varlist {

      foreach lname of newlist newvarlist {

      foreach lname of numlist numlist {

      The over-arching principle is that whatever is not allowed is forbidden,

      I know nothing in detail about logout or ttable2 (which you are asked to explain as community-contributed commands) but a loop starting


      Code:
      foreach group in new1 sand sean female highschool {
      has a better chance of being legal, except that the backslash before a local macro name is a no-no-no, although subtle unless you have read

      https://www.stata-journal.com/sjpdf....iclenum=pr0042

      or

      [U] 18.3.11 Constructing Windows filenames using macros

      Here is the latter, copied in its entirety.

      Stata uses the \ character to tell its parser not to expand macros.

      Windows uses the \ character as the directory path separator.

      Mostly, there is no problem using a \ in a filename.

      However, if you are writing a program that contains a Windows path in macro path and a filename in fname, do not assemble the final result as

      `path'\`fname'

      because Stata will interpret the \ as an instruction to not expand `fname'. Instead, assemble the final result as

      `path'/`fname'

      Stata understands / as a directory separator on all platforms.





      Comment

      Working...
      X