Announcement

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

  • Combining local macros in Stata

    I am trying to create a loop that copies specific files (which have a specific identifier at the end of the filename) from a list of folders to another folder using Windows shell commands. However, I am having trouble working with the local macros in Stata. I have tried troubleshooting this with a variety of different ways but get various issues.

    1) When I run the commands below:
    local folder "folder"
    local version "version10"
    di "C:\...\`folder'\*`version'.*"

    Stata shows this:
    C:\...`folder'\*version10.*

    It is unclear to me why the `folder' does not return value the assigned to the local macro and also why the backslash somehow disappears.

    2) I tried to get around this by combining the local macros as follows:
    local folder "folder"
    local version "version10"
    local path = "C:\..." + "`folder'" + "\*" + "`version'" + ".*"
    di "`path'"

    This works and returns the following path:
    C:\...\folder\*version10.*

    However, when I tried to create a loop to do this for all the folders as follows:
    local names "a" "b" "c" (These would be the folder names)
    local version "version10"
    foreach folder of local names {
    local path = "C:\..." + "`folder'" + "\*" + "`version'" + ".*"
    di "`path'"
    }

    I get a "too few quotes" error, even though this is the same exact command that I ran previously that returns the correct path.

    I would really appreciate any help with this.

  • #2
    You have hit the jackpot - your problems experience two of the more subtle annoying issues in Stata syntax. So there's a lot to learn here.

    The problem with (1) is that the backslash character
    Code:
    \
    has meaning to Stata: it will prevent the interpretation of any following character that has a special meaning to Stata, in particular
    Code:
    `
    will not be interpreted as indicating a reference to a macro.

    But all is not lost: Stata will allow you to use the forward slash character in path names on any operating system, and on Windows will take care of doing what must be done to appease Windows.
    Code:
    . local prefix batch42
    
    . display "Q:\HOME\`prefix'_filename"
    Q:\HOME`prefix'_filename
    
    . display "Q:/HOME/`prefix'_filename"
    Q:/HOME/batch42_filename
    So you can change your first example:
    Code:
    . di "C:/../`folder'/*`version'.*"
    C:/../folder/*version10.*
    Now as to the error in (3), I have example code below that shows what's necessary, but for the explanation, read the Statalist topic at

    https://www.statalist.org/forums/for...-list-question

    Code:
    . local names `" "a" "b" "c" "'
    
    . local version "version10"
    
    . foreach folder of local names {
      2. local path = "C:/../`folder'/*`version'.*"
      3. di "`path'"
      4. }
    C:/../a/*version10.*
    C:/../b/*version10.*
    C:/../c/*version10.*
    
    .
    Let me add that if this was a simple example of a longer problem, and you needed to stretch the list over several lines,
    Code:
    local testlist2 `" "bob sue" ///
        "bob" ///
        "sue bob ed" "'
    won't work, a string cannot span several lines. Instead, use the #delimit command.
    Code:
    #delimit ;
    local testlist2 `" "bob sue"
        "bob"
        "sue bob ed" ;
    #delimit cr

    Comment


    • #3
      Thank you so much! That was very informative. Thanks also for pointing out the issue with stretching the list over several lines (which I had to do). It saved me from a bit of frustration!

      I got it working now, but it I did end up needing to use the approach of combining the local macros with the "+" in order to create the file path, since windows does not appear to recognize paths with forward slashes.

      So the final code is:

      Code:
      #delimit ;
      local names `" "a"
      "b" "c" "' ;
      #delimit cr
      
      local version "version10"
      foreach folder of local names {
      local path = "C:\...\" + "`folder'" + "\*`version'.*"
      shell move "`path'" "C"\...\Destination"
      }

      Comment


      • #4
        I forgot that the goal of the code was to create a Windows command, not a Stata path, as you clearly stated in post #1 but which I forgot as I was composing my reply. You have my apologies, and my sympathy about Windows. Thank you for taking the time to post your final solution to your problem for future readers to learn from.

        Comment

        Working...
        X