Announcement

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

  • Trouble calling a global macro with foreach var of varlist command

    Hello. I wanted to use a global macro to assign a variable list, and called the list with the foreach var of varlist command. However, I am getting error messages.

    Interestingly, when I run the same codes with my existing do file, they works well. However, when I start a new do file with the codes, it is not working. I want to know what I missed.

    Thank you for your time.

    The error message is: "mpg trunk turn invalid name

    Here are my codes with the auto data:
    -------------------------------

    * data
    sysuse auto, clear

    * testing a loop command logging mode values
    tab mpg
    tab turn
    tab trunk

    * a list of variables
    global testvar "mpg trunk turn"
    di "$testvar"

    * generate an excel file
    putexcel set "test", replace
    putexcel A1=("This Excel file shows the mode values for the selected variables")

    * variable names
    putexcel A3=("Name")
    putexcel B3=("Mode")

    local varnamerow = 4

    foreach var of varlist "$testvar" {
          
          quietly ds `var'
          local varname r(varlist)
          putexcel A`varnamerow'=(`varname')
          
          quietly egen temp = mode(`var')
          quietly sum temp
          quietly local modeval = `r(mean)'
          putexcel B`varnamerow'=(`modeval')
          drop temp
          
          local varnamerow = `varnamerow' + 1
    }

    putexcel close


  • #2
    Remove the double quotes in the foreach var of ... line

    Comment


    • #3
      Hi Hemanshu,

      Thank you so much for your suggestion. Do you mean revising the code like this one?

      foreach var of varlist $testvar { ...

      It is not working; without the double quotes, the global macro calls the first value for each variable, I think.

      Here is the error message:

      is not a valid command name

      Comment


      • #4
        Hi Hemanshu,

        Thank you so much for your suggestion. Do you mean revising the code like this one?

        foreach var of varlist $testvar { ...

        It is not working; without the double quotes, the global macro calls the first value for each variable, I think.

        Comment


        • #5
          Code:
          foreach var of global testvar {
          
          }
          ---------------------------------
          Maarten L. Buis
          University of Konstanz
          Department of history and sociology
          box 40
          78457 Konstanz
          Germany
          http://www.maartenbuis.nl
          ---------------------------------

          Comment


          • #6
            Thank you Maarten. I don't know why but the suggested command,
            foreach var of global testvar { } is not working on my end. It gives an error message:        is not a valid command name r(199);

            Comment


            • #7
              Here is some code that works. Replace the part within the loop with whatever you need to do...

              Code:
              sysuse auto, clear
              global testvar mpg trunk turn
              
              foreach var of global testvar {
                  dis "processing `var' ..."
              }

              Comment


              • #8
                Hi Donghwan
                the code Maarten suggested works fine,
                the error you report isnt related to using -foreach var of global testvar {}-, but with the fact that you may have tried to copy and paste your code again.
                When you do that, some times, it will copy "empty space" characters that are different from "space". Thus creating an error.
                If you retype everything with the new code, it should work.
                F

                Comment


                • #9
                  Hi FernandoRios,

                  Thank you for your suggestion. I just realized that my codes are not working without the global macro. Like you said, even though I type "mpg trunk turn" instead of the global macro, I got the same error message.

                  I will start the new do file and retype the codes.

                  Also, thank you again Maarten and Hemanshu!

                  Edit: it works after deleting all spaces in the foreach codes. I truly appreciate your supports, Maarten, Hemanshu, and FernandoRios!
                  Last edited by Donghwan Gu; 07 Jul 2023, 09:26.

                  Comment

                  Working...
                  X