Announcement

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

  • User written ado, problem with specifying a file location

    Hi Statalist folks,

    I'm having some difficulty deciding how to let a user specify a file location as an option on a user written ado file. Here is the program so far...

    Code:
    program mybuddycode
        
        syntax varlist(min=2) [if] [in] [, fstub(string) floc(string) replace]
        
        // Check if file location (fstub) option provided. If not declare a default.
        if "`fstub'" == "" {
            local fstub = "buddy"
        }
        
        // Left commented out during testing.
        // export delimited `varlist' using "`floc'`fstub'_X.csv" `touse', `replace'
        
        di as result "Exported file to: `floc'`fstub'_X.csv, `replace'"
        
    end
    When not using the floc() option the output is what I would expect:

    Code:
    . mybuddycode comwant comthink alcdrunk
    Exported file to: buddy_X.csv,
    When using the floc option as thus I don't get usable output as thus:

    Code:
    . mybuddycode comwant comthink alcdrunk, floc(c:\data)
    Exported file to: c:\databuddy_X.csv,
    Changing the floc to include the last backslash this happens (also not usable):

    Code:
    . mybuddycode comwant comthink alcdrunk, floc(c:\data\)
    Exported file to: c:\data`fstub'_X.csv,
    My hunch is that this has been encountered in the past. But not luck digging it by by searching around. Any hints on how to solve this?

  • #2
    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

    Comment


    • #3
      More on this in the FAQ: https://www.stata.com/support/faqs/p...es-and-macros/

      Comment


      • #4
        The backslash is a escape character. You can see the explanation and solution to your problem in a Stata FAQ.

        Comment


        • #5
          Also http://www.stata-journal.com/sjpdf.h...iclenum=pr0042

          Comment


          • #6
            Thanks folks. Closing this out with updated code that solves the issues. Lets user write code with either forward or backward; also with or without slash at end of file location.

            Code:
            program mybuddycode
                
                syntax varlist(min=2) [if] [in] [, fstub(string) floc(string) replace]
                
                // Check if file location (fstub) option provided. If not declare.
                if "`fstub'" == "" {
                    local fstub = "buddy"
                }
                
                // Check if file location (floc) option specified.
                // If specified replace backslashes with forward slashes.
                // Check if user provided last slash, if not then add one.
                if "`floc'" != "" {
                    local floc = subinstr("`floc'","\","/",.)
                    if strpos(strreverse("`floc'"),"/") != 1 {
                        local floc = "`floc'/"
                    }
                }
                
                // Left commented out during testing.
                // export delimited `varlist' using "`floc'`fstub'_X.csv" `touse', `replace'
                
                di as result "Exported file to: `floc'`fstub'_X.csv, `replace'"
                
            end

            Comment

            Working...
            X