Announcement

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

  • Loop through global variable and still obtain a global variable

    I have this code:
    Code:
    global sets = "set1 set2"
    foreach set of global sets {
    global set = "`set'"
    display "*** POTENTIAL APPLICANT SET: ${set} ***"
    Is there a way to avoid the:
    Code:
    global set = "`set'"
    That is, to loop through global variables and still obtain a global variable?

  • #2
    In Stata terms this is about global macros, which aren't considered to be variables.

    You get the same output with

    Code:
    global sets = "set1 set2"
    foreach set of global sets {
    display "*** POTENTIAL APPLICANT SET: `set'***"
    }
    There is no gain -- for what we would see here -- in putting each value into a global and then taking it out again.

    Cue Clyde Schechter on the dangers of global macro. I use Stata just about every day, including programming commands, and haven't memorably used a global macro for years unless answering a question on them.
    Last edited by Nick Cox; 18 Oct 2022, 09:44.

    Comment


    • #3
      Code:
      . global sets set1 set2
      
      . local n : word count $sets
      
      . forvalues sn=1/`n' {
        2. global set : word `sn' of $sets
        3. display "*** POTENTIAL APPLICANT SET: ${set} ***"
        4. }
      *** POTENTIAL APPLICANT SET: set1 ***
      *** POTENTIAL APPLICANT SET: set2 ***
      
      .

      Comment


      • #4
        Code:
        There is no gain -- for what we would see here -- in putting each value into a global and then taking it out again.
        I write code with nested loops.
        One loop through my hypotheses, and a loop inside through my dependent variables, etc.
        When I need to do debug, I would like to run the inner code (the code within all the nested loops), and specify "debug" parameters for the variables that are looped over.
        But if those variables are local, in the code they are specified as `var', and I cannot re-specify them in the STATA console as they would be out-of-scope.
        So I need global variables.
        Does it make sense?
        How do you manage to do it in those cases?

        Code:
        local n : word count $sets
        What is that?
        `help word` give me the help for the string function `word(s,n)`. Am I looking at the correct help section for this?



        Comment


        • #5
          You can execute your code with Execute (include) -- that option is available from both a button on the top right of your do-file editor window, as well as from the menu bar: View > Do-file Editor > Execute (include)

          Then, any local macros you use are within the scope of the Stata console even after the code stops executing.

          Comment


          • #6
            Code:
            help macro 
            
            help extended fcn
            is the easy answer to your second question.

            Your main question is harder. I debug in various ways. For a serious command I have a test script as a do-file that reads in data, etc. If I need to pass arguments to the do-file, I can do that directly, but I prefer the do-file to be self-contained.

            Once again, please: a global macro is not considered to be a variable in Stata. If you talk about global variables, people can work out what you mean, but it's best just to use Stata terminology.

            More at https://www.stata.com/statalist/arch.../msg01258.html

            Comment


            • #7
              You can execute your code with Execute (include) -- that option is available from both a button on the top right of your do-file editor window, as well as from the menu bar: View > Do-file Editor > Execute (include)
              Thank you.

              I don't have that button in my STATA 17, and the menu entry seems to be on "Tools -> Execute (include)" in the do-file editor.

              Thank you. So I used a solecism

              Comment


              • #8
                Ah, I'm on Stata 17 for Mac. Maybe the layout is a little different on other operating systems. Here is a picture of what the top-right corner of my do-file editor window looks like, and the button I speak of:
                Click image for larger version

Name:	Screenshot 2022-10-25 at 6.56.20 PM.png
Views:	1
Size:	84.6 KB
ID:	1686699

                Comment


                • #9
                  This is mine on Fedora Workstation 36

                  Click image for larger version

Name:	Screenshot from 2022-10-25 15-59-14.png
Views:	1
Size:	10.9 KB
ID:	1686706

                  Comment


                  • #10
                    And what does clicking on that right-most button do?

                    Comment

                    Working...
                    X