Announcement

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

  • Line by line execution of code in do-file works - calling the same do-file from another do-file does not

    The code below runs without error message if I execute it line by line.

    Code:
    ------------------------------------begin example.do--- 
    
    clear
    sysuse auto
    
    /* macro 1 */
    local makeList `" ///
        "Buick Century" ///
        "Pont. Sunbird" ///
        "BMW 320i" ///
        "'
    
    /* macro 2 */
    * local makeList `" "Buick Century" "Pont. Sunbird" "BMW 320i" "'
    
    foreach i of local makeList {
            
            di "`i'"
    }
    
    ------------------------------------end example.do---
    If I execute this do-file from another do-file like:

    Code:
    do example
    I get an invalid syntax r(198); error message.

    If I comment the "macro 1" version of makeList above and use the "macro 2" version of makeList instead, the code runs without error message.

    I would like to use the "macro 1" version for better readability. Is there a work-a-around?

  • #2
    You don't need the compound double quotes around the quoted items:

    Code:
    local makeList /// 
        "Buick Century"  /// 
        "Pont. Sunbird"  ///
        "BMW 320i"
    Or you could change the delimiter a semicolon.

    Comment


    • #3
      The version of the local command given in post #2 does not produce the same results as the version in macro 2 of post #1. This is sort of a known but subtle gotcha that I find difficult to retain the logic for clearly in my mind, much less explain coherently. All I can say is that the syntax for the local command (look at help local) accepts exactly one string, enclosed at least in double quotes, or optionally in compound double quotes, which are required in this case, since the string itself contains double quotes. Please don't ask me to explain that more coherently, it hurts my head, since I know that generally you can omit the quotation marks with no ill effect, even though the syntax suggests they are required.

      Code:
      . do "/var/folders/xr/lm5ccr996k7dspxs35yqzyt80000gp/T//SD17699.000000"
      
      . local makeList2 `" "Buick Century" "Pont. Sunbird" "BMW 320i" "'
      
      . local makeList3 ///
      >     "Buick Century"  ///
      >     "Pont. Sunbird"  ///
      >     "BMW 320i"
      
      . macro list _makeList2 _makeList3
      _makeList2:      "Buick Century" "Pont. Sunbird" "BMW 320i"
      _makeList3:     Buick Century" "Pont. Sunbird" "BMW 320i
      
      .
      end of do-file
      Added in edit: with that said, the reason macro 1 in post #1 fails is because within a quoted string, the line continuation characters "///" are treated as plain text devoid of significance, so the line is not continued and it looks like an unclosed quoted string. Changing the command delimiter as Scott suggests in post #2 works as expected.
      Code:
      . do "/var/folders/xr/lm5ccr996k7dspxs35yqzyt80000gp/T//SD17699.000000"
      
      . #delimit ;
      delimiter now ;
      . local makeList1 `"
      >     "Buick Century"
      >     "Pont. Sunbird"
      >     "BMW 320i"
      >         "' ;
      
      . #delimit cr
      delimiter now cr
      . macro list _makeList1
      _makeList1:      "Buick Century" "Pont. Sunbird" "BMW 320i"
      
      .
      end of do-file
      Last edited by William Lisowski; 03 Jul 2017, 12:58.

      Comment


      • #4
        Good catch.

        Comment


        • #5
          Thanks! That solves my problem in that it is a valid work-a-round.

          However this behavior of Stata is unfortunate (in my opinion) as it somehow interrupts the flow of developing code. Testing code line by line is essential (for me in Stata) given a lack of powerful debugging tools in Stata. If I then want to run the complete do-file after having tested it line by line I have to change the code again since the #delimit solution only seems to work if I run the complete do-file and not line by line.

          Comment


          • #6
            When I responded I passed on your comment about running the command line-by-line because I was unable to figure out what precisely you were doing. Your comment in post #5 reinforces my failure to understand, as it were.
            Code:
            local makeList `" ///
                "Buick Century" ///
                "Pont. Sunbird" ///
                "BMW 320i" ///
                "'
            When I have this code among others within the Do-file Editor window and I select these five lines and hit the "Do" button (or select Do from the File menu), it will fail because the selected lines are copied into and run from a temporary do-file. And if I select these five lines, copy them and paste them the into the Command window, they fail because continuation lines are not supported in the Command window.

            I am running Stata 14.2 for Mac. Can you tell us what version of Stata you are using and precisely how you are running those five lines that they do not yield a syntax error?

            In general, "line-by-line" testing of do-files in Stata fails once your do-file includes local macros, because using the "Do" button to run a selection of your lines copies them to a temporary do-file, and any local macros vanish at the completion of the do-file. And copy-and-paste into the Command window runs afoul of the different conventions in the Command window - which is intended for interactive support - and the Do-file interpreter - into which features like line continuation syntax have been grafted.

            There are other debugging tools documented in the Stata Programming Reference Manual PDF included with your Stata installation. None of them however form an "integrated development environment" of the sort common with application programming languages.

            Comment


            • #7
              I am using Stata 13.1 for Mac. My editor is Sublime Text 3 (Build 3126) using the Stata Enhanced package (https://packagecontrol.io/packages/Stata%20Enhanced) to send code to Stata. Using this combination, i.e. sending code "line-by-line" from Sublime to Stata, I do not get a syntax error.

              I just tried Stata's do-file editor to test this issue and then I also get the syntax error.

              My conclusion is that Stata is actually "consistent" and that my incorrect formulation of the macro is somehow corrected by the Stata Enhanced package. I will point the developer to this thread.

              Comment


              • #8
                David Florysiak
                I've tried Stata Enhanced with Stata 14.2, but I noticed that local macros continue to exist even after the end of the do file. Does this happens to you too? This is a major limitation of using ST3 as do-file editor.

                Comment


                • #9
                  Can you please post the code/an example that I can test on my machine.

                  Comment


                  • #10
                    Sure. For example:

                    Code:
                    clear all
                    macro drop _all
                    set obs 5
                    tempvar a
                    gen `a' = runiform()
                    su `a'
                    You'll see that you're left with the temporary variable `a' (something like __000000) in your dataset.

                    Using StataEnhanced is like executing commands line-by-line directly from the command window, which, in my opinion, is not what one expects when they use a do-file.

                    Comment

                    Working...
                    X