Announcement

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

  • how to disperse a local macro across multiple lines in a .do file in stata 17

    Dear Statalist,

    I have functioning code that uses a macro similar to the following:

    Code:
    local lname `"     "xxx" "yyy" "zzz" "aaa" "bbb" "'
    
    foreach y of local lname {
        display "`y'"
    }
    output:

    xxx
    yyy
    zzz
    aaa
    bbb



    However there are a lot of xxx, yyy and zzz's (diagnostic codes, not available in icd command suite) and in the stata do file I want to wrap the code across multiple lines to make it neater.

    Using the /// doesn't work (invalid syntax), and neither does #delimit . I need to have the double quotations when defining the local macro as described here:
    https://www.statalist.org/forums/for...-local-command


    Code:
    #delimit
    local lname  `"     "xxx" "yyy" "zzz" 
                        "aaa" "bbb" "'
    #delimitcr
    
    foreach y of local lname {
        display "`y'"
    }
    Output: (nothing)

    Is there a way of solving this problem?

    My code for a nested loop (as described here: https://www.statalist.org/forums/for...each-and-local) works if I don't create line breaks in the do file


    Kind regards,
    Ben

  • #2
    I am surprised that you didn't get an error message as

    Code:
    #delimit
    must be followed by cr or ;.

    I often use a loop and here consider

    Code:
    foreach x in xxx yyy zzz aaa bbb {
         local X `X' `x'
    }
    
    . di "`X'"
    xxx yyy zzz aaa bbb
    
    .
    Naturally in this case you could just define the macro in one line, but I presume that your real example is more complicated.

    Comment


    • #3
      To address the code you wrote:
      Code:
      . do "/var/folders/xr/lm5ccr996k7dspxs35yqzyt80000gp/T//SD49426.000000"
      
      . #delimit
      delimiter now ;
      . local lname  `"     "xxx" "yyy" "zzz" 
      >                     "aaa" "bbb" "'
      > #delimitcr
      > 
      > foreach y of local lname {
      >     display "`y'"
      > }
      > 
      
      end of do-file
      
      .
      we see (a) that the #delimit command did indeed set the delimiter to a semicolon and (b) you never included a semicolon in your code, and so Stata just sucked up everything to the end of the do-file without ever feeling it had found the end of the command.

      If we add a semilcolon
      Code:
      . do "/var/folders/xr/lm5ccr996k7dspxs35yqzyt80000gp/T//SD49426.000000"
      
      . #delimit
      delimiter now ;
      . local lname  `"     "xxx" "yyy" "zzz" 
      >                     "aaa" "bbb" "' ;
      
      . #delimitcr
      Unknown #command
      . foreach y of local lname {
      >     display "`y'"
      > }
      > 
      
      end of do-file
      
      .
      we see (c) that #delimitcr is not the correct syntax. And if we fix that error
      Code:
      . do "/var/folders/xr/lm5ccr996k7dspxs35yqzyt80000gp/T//SD49426.000000"
      
      . #delimit
      delimiter now ;
      . local lname  `"     "xxx" "yyy" "zzz" 
      >                     "aaa" "bbb" "' ;
      
      . #delimit cr
      delimiter now cr
      . 
      . foreach y of local lname {
        2.     display "`y'"
        3. }
      xxx
      yyy
      zzz
      aaa
      bbb
      
      .
      we now see success.

      Comment


      • #4
        It works, thanks very much Nick Cox and William Lisowski !

        Comment

        Working...
        X