Announcement

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

  • Cd with local and an other do file

    Hi,
    I'm trying to use local in my CD but it doesn't work.

    I can do this :

    Code:
    local directory="C:\Users\...\ABC"
    local date_export="20-10-14"
    cd "`directory'\Exports/`date_export'"
    It's working, but i want to use thoses local in an other do file. Something like this :


    Code:
    local directory="C:\Users\....\ABC"
    local date_export="20-10-14"
    
    do Test
    And in the do Test i've this cd "`directory'\Exports/`date_export'"

    I want to CD to : C:\Users\...\ABC\Exports\20-10-14

    So there is the error :

    Code:
    . local directory="C:\Users\...\ABC"
    . local date_export="20-10-14"
    
    . do Test. 
    . cd "`directory'\Exports/`date_export'"
    unable to change to \Exports/
    r(170);
    
    end of do-file
    r(170);
    
    end of do-file
    I have a main do file with the local and then many others do files with alls commands. I only want to change the main do file on date changes

    Thanks !

  • #2
    You can pass arguments to do-files. Typing

    Code:
    do Test "`directory'" "`date_export'"
    will make the locals' contents (in this case, the directory and the date) available in Test.do in local macros 1 and 2. You can get the arguments passed to a do-file into named local macros. In Test.do, type

    Code:
    args directory date_export
    ...
    near the top of the do-file. You can then refer to `directory' and `date_export'. If the contents of the locals that you are passing contain spaces, remember to use double-quotes.

    Another alternative is the include command. But that requires a separate file in which all locals are defined; you might not want that.

    Yet another option are global macros. Those are a bit dangerous and they should be used with caution.

    On a slightly different note, you should avoid backslashes in local (and global) macros. They can lead to problems because they have a special meaning in Stata.

    Comment


    • #3
      The local must be defined wherever it is used, so inside the do file not outside the do file. That is what local means, defined locally.

      But see

      Code:
      help include
      https://www.stata-journal.com/articl...article=pr0047


      for a way to do what you want as well as (say) https://www.stata-journal.com/articl...article=dm0102





      Comment


      • #4
        a local macro is local. If you open Stata and type commands in the command line, you are in one session and local macros created like that exist there. If you run a .do file, that is another level, and locals don't persist at that level. This is deliberate, as this way there cannot be a conflict between locals created at the main session and in the .do file.

        OK, so that is why what you do does not work. So what can you do? You can add arguments when you do a do-file. You can type something like do test "C:\Users\...\ABC" "20-10-14", and what that will do is create the local macro 1 in the do-file test.do containing "C:\Users\...\ABC" and a local macro 2 containing "20-10-14". You can use args inside that do file, to give the locals nicer names.

        So my do-file would look something like this:

        Code:
        args directory date_export
        cd "`directory'/`date_export'"
        
        do interesting stuff
        And than call that do-file

        Code:
        do test "C:\Users\...\ABC" "20-10-14"
        ---------------------------------
        Maarten L. Buis
        University of Konstanz
        Department of history and sociology
        box 40
        78457 Konstanz
        Germany
        http://www.maartenbuis.nl
        ---------------------------------

        Comment

        Working...
        X