Announcement

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

  • Issue with local macros and invalid syntax

    Dear all,
    I am quite new at Stata. I have to work on a paper by Galor, Özak and Sarid(2020) and the original code contains this:

    Code:
    local second = `"(secondgen ==1 & parcntr~="")"'
    local onehalf = `"(firstgen==1 & agearrive<=5 & iso_self~="")"'
    
    gen onesec = iso_self if `onehalf'
    replace onesec = parcntr if `second'
    It always returns "invalid syntax" for the "generate" line, when the exact same code works on my teammates' computers. We are working on the same version of Stata SE 18.0 (on our university server).

  • #2
    I have comments on two levels, quite different.

    The main level is what is wrong with the syntax and my answer is nothing that I can see. But I had to invent a silly sandbox dataset to run the code as staring at the code didn't show up any obvious errors.

    Code:
    clear
    set obs 1
    
    gen firstgen = 1
    gen secondgen = 1
    gen agearrive = 3
    gen iso_self = "toad"
    gen parcntr = "frog"
    
    local second = `"(secondgen ==1 & parcntr~="")"'
    local onehalf = `"(firstgen==1 & agearrive<=5 & iso_self~="")"'
    
    mac li
    
    gen onesec = iso_self if `onehalf'
    replace onesec = parcntr if `second'
    Here are my results. I edited the output to remove details irrelevant to the question.

    Code:
    . mac li
    _onehalf:       (firstgen==1 & agearrive<=5 & iso_self~="")
    _second:        (secondgen ==1 & parcntr~="")
    
    
    . gen onesec = iso_self if `onehalf'
    
    . replace onesec = parcntr if `second'
    (1 real change made)
    In general, what computer you're using is immaterial to this kind of question. So, I am reduced to one hypothesis: that somehow your version of the code contains invisible mark-up characters that arise from copy-and-paste. (That's something that can bite with this forum's software too.)

    The second level is more personal. Despite your link (thanks!) I can't quickly locate their code, but either way, coding up if conditions like this is something I would never do. First up there is the need to wrestle with compound double quotes. Next, and more fundamental, is a reluctance to write that way because I don't think it makes code easier to write, to follow, to maintain or to debug. But some coders might jump quite the other way. If your main language or some coding dogma obliges or encourages early definition of constants, that's doubly true.

    Comment


    • #3
      Thank you for the answer!

      I managed to make that part work, but only by executing the whole do-file at once... As the file is very long, it is not practical at all.

      Comment


      • #4
        I can't follow that. It doesn't add up to an explanation of why you couldn't make code work while others could, so this all remains mysterious.

        The example in #2 is silly but is self-contained: does it work for you? (Check any do-file for special characters using a good text editor or official Stata command hexdump.)
        Last edited by Nick Cox; 19 Mar 2025, 15:32.

        Comment


        • #5
          Here is one other possibility: OP is running the code piece-by-piece on their computer, but as the full do-file on their teammates' computers. When OP runs the full do-file on their own computer, the code works.

          The issue is likely to be that when executing the code piece-wise, local macro definitions do not stay in memory. So if this line is executed by itself:
          Code:
          gen onesec = iso_self if `onehalf'
          Stata does not know what onehalf is, and evaluates that statement as
          Code:
          gen onesec = iso_self if
          which would produce
          Code:
          invalid syntax
          r(198);
          If my guess is right, all OP needs to do is to use the "Execute selection (include)" option when running the code piece-wise. You can find this option by clicking the arrow next to the "Do" button on the right corner of the toolbar at the top of the do-file editor window. This option keeps macros etc in memory.

          Comment


          • #6
            I think Hemanshu Kumar has filled in the gap here. I should have thought of that myself!

            For more guidance, however, see https://journals.sagepub.com/doi/pdf...36867X20931028 which gives a fairly complete story.

            Comment


            • #7
              Hemanshu Kumar Thank you! It finally works.

              Comment

              Working...
              X