Announcement

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

  • Getting file names without brackets in a neat way

    Hello Statalisters!

    This may seem like a trivial question, but I want an efficient way to do my task and I can't seem to find it. In the following code:

    Code:
    foreach wa of local wave {
        
        // The 2010 wave has a different directory than the rest.
        if `wa' != 2010 {
            local data_`wa' "`path'/`wa'/Stata"
        }
        else local data_`wa' "`path'/`wa'/STATA/methodology"
        
        foreach tab of local tables {
            foreach name of local `tab' {
                local wrong_files "`data_`wa''/`name'.dta"
                capture: confirm file `wrong_files'
                
                if !_rc {
                    local `tab'`wa': dir "`data_`wa''" files "`name'.dta", respectcase
                }
                
                else {
                    continue
                }
            }
        }
    }
    What I wish to obtain are macro locals called `tab'`wa' (suppose `tab' contains country names and `wa' years) each containing the name of a single and unique file stored in several standardized folders. The reason I use macro locals and not filenames directly is because the actual database names are very different and there is no way to use a stub to use them (and I can't rename them neither), so using local macros is a useful way to do this. However, once I created all the desired `tab'`wa' local macros, when I use the command :

    Code:
    foreach wa of local wave {
        use "`data_`wa''/`usa`wa''", clear
    }
    I get the following message:

    = use "D:/XXXXX/Stata/"4572q.dta"", clear (edited to anonymize the results)
    invalid '4572q.dta'

    Which means the local `usa`wa'' has indeed double quotes that got 4572q.dta. is there a way I can ignore, or even better, remove these double quotes? Preferably I'd like something efficient and neat, so if it could suppress double quotes from the start rather than through commands like subinstr it would be perfect. Thank you all guys!
    Last edited by Julia Simon; 26 Jul 2023, 08:34.

  • #2
    Perhaps something like this might help?

    Code:
    local `tab'`wa': list clean `tab'`wa'

    Comment


    • #3
      Hemanshu: It worked! Thanks!

      Comment


      • #4
        I haven't tested this because it is difficult to set up directories and folders resembling what you are working with. But I think you can accomplish the same thing by changing your definition of local macro data_wa, removing the quotes there. In all but the most archaic versions of Stata, it is generally not necessary to wrap the definition of a macro in quotes, and it often creates problems when you do so. That is because Stata tries to strip the outermost quotes from the material defining a local macro, but sometimes it does so incompletely or incorrectly. So quotes should be avoided when defining local macros unless they are needed to signal to Stata that the material contained in them, despite containing one or more blank spaces, should be handled as if it were a single word. (And I should add that it is problematic if the first element of a macro is a quoted series of tokens that you want treated as a single word--putting the elements in a different order so that the first one really is a single word should be tried if at all possible.)

        Added: To be clear, I am referring above only to the definition of the macros. Leave out quotes as much as you can there. But add quotes where appropriate when you use the macros. Thus:
        Code:
        local myfilename silly file name with lots of spaces
        use `"`myfilename'.dta"'
        Last edited by Clyde Schechter; 27 Jul 2023, 09:52.

        Comment

        Working...
        X