Announcement

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

  • Getting STATA to loop phrases (2 or more words with spaces in between)

    Hello,


    I'm trying to construct a foreach loop where I am using a few words as the phrase to loop over to specify the regression variables, graph title and saved graph name. However, there is an invalid syntax error, which I think has to do with STATA either not recognizing the "" or the spaces. How do I specify that correctly?


    Here is an example below that would result in that error:

    Code:
    foreach `x' in "y x" "y z" {
    
    reg `x' i.year
    
    twoway (connected `x') ///
    , title(`x'- model, size(medsmall))
    
    graph export C:\Documents\model-diagnostic\`x'- model.pdf, replace
    
    }
    Here is what I want it to do in the first loop:

    Code:
    reg y x i.year
    
    twoway (connected y x) ///
    , title(y x- model, size(medsmall))
    
    graph export C:\Documents\model-diagnostic\y x- model.pdf, replace
    secondarily, would STATA also have trouble with there being a space in the graph export file name?

    Thank you in advance!
    I am using Stata SE x64 ver 13.1 with Win 7 x64 and with 8 GB of ram.

  • #2
    Your loop should start

    Code:
    foreach x
    given what you type elsewhere. Also, file names containing spaces need to be given within “ “.

    The backslash before a local macro name will bite you too

    Code:
    search backslash
    should find explanations.

    A matter of taste not syntax: I would never use the local macro name x for a local macro containing both response and predictor names. I might use say vars.

    Comment


    • #3
      Thanks Nick. That was a great help. Maybe this is helpful to someone in the future, but here's some generic code that worked for me


      sysuse auto, clear


      foreach vars in "price mpg" "price rep78" {

      reg `vars'

      twoway (connected `vars'), title(`vars'- model, size(medsmall))

      graph export C:\Documents\Model_diagnostics\`vars'-model.pdf, replace

      }
      I am using Stata SE x64 ver 13.1 with Win 7 x64 and with 8 GB of ram.

      Comment


      • #4
        This thread was helpful to me.
        The bit I'm struggling with is saving/exporting the files with the phrases (because my phrases are often long and contain special characters). Is there are straightforward way to use the phrases when running the regression commands but use a simple sequence (e.g. reg1, reg2) for saving the files?
        Hope that makes sense.
        With thanks

        Comment


        • #5
          Originally posted by tim bradshaw View Post
          Is there are straightforward way to use the phrases when running the regression commands but use a simple sequence (e.g. reg1, reg2) for saving the files?
          Yes. You can try something like this, which creates a second local macro, i, that starts at 1 and increases by 1 with each iteration of the loop. (This example is untested.)
          Code:
          local i = 1
          sysuse auto, clear
          foreach vars in "price mpg" "price rep78" {
          reg `vars'
          twoway (connected `vars'), title("`vars'- model", size(medsmall))
          graph export C:\Documents\Model_diagnostics\reg`i'.pdf, replace
          local ++i
          }
          David Radwin
          Senior Researcher, California Competes
          californiacompetes.org
          Pronouns: He/Him

          Comment


          • #6
            That did the job perfectly, thanks so much.

            Comment

            Working...
            X