Announcement

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

  • Using a macro name in a directory

    I am trying do a loop where I put graphs in different directories on my computer.
    Code:
    foreach name in "Bedtextiel" "Gebak" "Kantoorart" {
    cd \\cbsp\productie\Primair\CPI\Werk\50PidMappen\Jvbg\CPI\DatasetHEMA\StataFiles
    do DataVoorbereiden`name'.do
    do PrijsIndicesSimple.do
    cd \\cbsp\productie\Primair\CPI\Werk\50PidMappen\Jvbg\CPI\DatasetHEMA\Grafieken\`name'
    
    format jevon_mtom chained_jevon dutot_mtom chained_dutot guv_mtom chained_guv laspeyres_mtom chained_laspeyres paasche_mtom chained_paasche fisher_mtom chained_fisher chained_tornqvist %3.0f
    xtline chained_jevon chained_dutot chained_laspeyres chained_paasche chained_fisher chained_tornqvist chained_guv if product_id == 1, byopts(title("`name'") subtitle("Diverse chained month-to-month prijs indices (EAN)"))
    graph export "`name'SimpeleIndicesEAN.pdf", as(pdf) replace
    }
    The program will not run the follwing line:
    Code:
     cd \\cbsp\productie\Primair\CPI\Werk\50PidMappen\Jvbg\CPI\DatasetHEMA\Grafieken\`name'
    I tried using
    Code:
      "`name'"
    at the end, but no luck. If I remove this line the code runs like it is supposed to.

    Any suggestions are welcome, thank you

    Johan

  • #2
    Try:
    Code:

    Code:
    cd `"\\cbsp\productie\Primair\CPI\Werk\50PidMappen\Jv bg\CPI\DatasetHEMA\Grafieken\`name'"'
    also, you don't mention the OS or version of Stata you are using, but I always use forward slashes in filepaths because they work across all the platforms or OSs I use (even Windows understands forward slashes in that C:\Documents\Foo translates to the URI: file:///C:/Documents/Foo) and the backslash can be an escape character.

    - Eric Booth
    Eric A. Booth | Senior Director of Research | Far Harbor | Austin TX

    Comment


    • #3
      The problem is that the Stata parser interprets the sequence \` as a literal ` character rather than the opening left quote of a local macro reference. This is a commonly occurring problem. The simplest solution is to use / instead of \ as your directory separator. (It does not matter that Windows expects to see \; Stata takes care of that when it interfaces with Windows for file handling.)

      Alternatively, you can use \\` instead of \`. The parser will interpret \\ as a literal backslash character (\) and then recognize ` as the start of a macro reference.
      Last edited by Clyde Schechter; 11 Feb 2016, 09:05. Reason: Add a second paragraph and correct typos.

      Comment


      • #4
        See also

        SJ-8-3 pr0042 . . . . . . . Stata tip 65: Beware the backstabbing backslash
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
        Q3/08 SJ 8(3):446--447 (no commands)
        tip to use forward slash instead of backslash for
        directory and file names

        http://www.stata-journal.com/article...article=pr0042

        Comment


        • #5
          Thanks for the quick responses, I will incorporate this in my code.

          Comment

          Working...
          X