Announcement

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

  • how to create tempfile named according to value of a local in a loop?

    Hi!

    I have 19 files, and want Stata to create a tempfile for each one, but named differently.
    How may I create a tempfile named according to the actual value of a local in a loop?
    What I do is a filesearch for some datasets, change them somehow (mycode), and then (via some regexr) create a local containing the tempfile name, respectively.

    Here's some (pretty nonsense) example for the sake of illustration. This is - I guess - a more a general question, and not so much depending on specific data, anyway:

    Code:
    input filename
    "StudyX_spWork_R_12-0-1.dta"
    end
    save "StudyX_spWork_R_12-0-1.dta"
    clear
    
    input filename
    "StudyX_spTravel_T_12-0-1.dta"
    end
    save "StudyX_spTravel_T_12-0-1.dta"
    clear
    
    input filename
    "StudyX_spSports_S_12-0-1.dta"
    end
    save "StudyX_spSports_S_12-0-1.dta"
    clear
    
    input filename
    "StudyX_Z_12-0-1.dta"
    end
    save "StudyX_Z_12-0-1.dta"
    
    qui filesearch *_sp*, dir($pathin)
    foreach file in `r(filenames)' {
    preserve di "create tempfile of `file'" qui use $pathin`file', clear qui include $pathdo/mycode
    local tmp = regexr(regexr("`file'", "^StudyX_", ""), "_12-0-1.dta$", "") di "designated name for tempfile: `tmp'" tempfile `"`tmp'"' save $pathout/`tmp', replace restore
    }
    With
    Code:
    set trace on
    , I can see, all but one line are executed well - why not the
    Code:
    save $pathout/`tmp', replace
    ? The error message states
    "_spWork invalid name
    , below tempfile `"`tmp'"' the (correct) traced result is
    tempfile `"spWork"'
    , though!
    Attached Files
    Thank you for reading (and some reply)
    Using Stata 16.1
    Extractions (-dataex-) of the data I'm working with is impossible, sorry!

  • #2
    Well, writing the last lines of the above posting (#1), I figured out: No need to worry! Due to the syntax in the help entry (attached above), I supposed the use of a local value must not be used as name of a tempfile. But ...
    Code:
    tempfile `tmp'
    save $pathout/`tmp', replace
    ... do work well!

    (I decided to post the issue (#1), however, since there might be users having the same question (or barrier?) in mind - and, the post was almost done .)

    The issue now is: the tempfiles created in one .do are not found by the (e.g.) use `spWork' in another one! Since, I use the command include (and not do or run), the use of different .do shouldn't matter, should it?
    Last edited by Franz Gerbig; 28 Jul 2020, 04:30.
    Thank you for reading (and some reply)
    Using Stata 16.1
    Extractions (-dataex-) of the data I'm working with is impossible, sorry!

    Comment


    • #3
      Originally posted by Franz Gerbig View Post
      The issue now is: the tempfiles created in one .do are not found by the (e.g.) use `spWork' in another one! Since, I use the command include (and not do or run), the use of different .do shouldn't matter, should it?
      OK, got it! Of course, the issue is not the
      Code:
      include
      , but the single quotes in the
      Code:
      save $pathout/`tmp', replace
      ! With compound* single quotes ... it works!
      Code:
      save $pathout/``tmp'', replace
      Thx anyway - in case, you have started a reply, yet!

      *Is that the right term? I'm somewhat lost between single, double, compound, nested, and some combination of those quotes (cf
      Code:
      h quotes
      ).
      Thank you for reading (and some reply)
      Using Stata 16.1
      Extractions (-dataex-) of the data I'm working with is impossible, sorry!

      Comment


      • #4
        ref #3
        Code:
        save $pathout/``tmp'', replace
        On MS Windows (with the environment variable TEMP set) the code above should not run if following
        Code:
        tempfile `tmp'
        because Stata tempfiles are saved in tmpdir and reference to a tempfile contain the full path of the temporary file:
        Code:
        tempfile test
        display "`test'"
        display "`c(tmpdir)'"


        The following example may help:
        Code:
        *** dofile names.do **********
        
        sysuse auto
        
        local names `c(Wdays)'
        
        foreach name of local names {
            
            tempfile `name'
            
            save "``name''"        
        }
        
        ******************************
        Code:
        include names.do
        
        local names `c(Wdays)'
        
        foreach name of local names {
        
            mac dir _`name'
            dir "``name''"        
        }
        Last edited by Bjarte Aagnes; 28 Jul 2020, 08:48.

        Comment


        • #5
          somewhat late, but ... thanks a lot Bjarte!
          Thank you for reading (and some reply)
          Using Stata 16.1
          Extractions (-dataex-) of the data I'm working with is impossible, sorry!

          Comment

          Working...
          X