Announcement

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

  • Can I Autogenate savefile names using another another filename as a "base"

    Hi
    I'm new to Stata. I have been looking in the manual and the forums to see if I could find an answer my question, but unfortunately without luck. I hope you can help me.

    I am currently importing delimited CSV files. I have made a do file that imports, makes a log file and saves the data twice - one save immediately after import and one save (under a different name) after Ive renamed and dropped a number of variables.

    This means for every CSV file I import I get 2 data files that each need a name which is based on the CSV file I import (please see code below for clarification.)

    Right now I am manually changeing the names of my save and log files. Its all the red numbers that I change to be the same as the green number. This works fine, but I need to import several hundred files so I would like to automate as much of the proces as possible.

    Is there a way where I can autogenerate these names? If I could just write the number one time instead of 4 it would make a big difference.


    clear
    import delimited ".....Session102.csv", delimiter(tab) varnames(nonames) rowrange(6)
    save "....\KGC_05_Session_102.dta", replace
    capture log close
    log using "....\KGC_05_Session_102log.txt", text replace
    cd "....\KGC 05"
    use KGC_05_Session_102.dta, clear
    set more off
    My do file runs.....and I end it with
    save "KGC_05_Session_102_cleaned ", replace


    I hope you can help me.
    Thanks in advance.

  • #2
    Sure, one way to do it is to save the number as a macro:

    Code:
    local number 102
    
    clear
    import delimited ".....Session`number'.csv", delimiter(tab) varnames(nonames) rowrange(6)
    save "....\KGC_05_Session_`number'.dta", replace
    capture log close
    log using "....\KGC_05_Session_`number'log.txt", text replace
    cd "....\KGC 05"
    use KGC_05_Session_`number'.dta, clear
    set more off
    *My do file runs.....and I end it with
    save "KGC_05_Session_`number'_cleaned ", replace
    Stata/MP 14.1 (64-bit x86-64)
    Revision 19 May 2016
    Win 8.1

    Comment


    • #3
      You can even automate reading the files - if all of the CSV files are in the same directory, you can do something like

      Code:
      local filenames : dir . files ".csv"
      foreach F in `filenames' {
             import delimited "`F'" , delimiter(tab) varnames(nonames) rowrange(6)
             local number : subinstr("`F'","....Session",,"",.)
             local number : subinstr("`number'",".csv","",.)
             save "KGC_05_Session_`number'.dta", replace
             ...
      }
      Where you will probably have to be a bit more careful than I have in pulling out the -number-, but you get the idea.

      hth,
      Jeph

      Comment


      • #4
        Thanks a lot - This is Awesome :-)

        Comment


        • #5
          Originally posted by Jeph Herrin View Post
          You can even automate reading the files - if all of the CSV files are in the same directory, you can do something like

          Code:
          local filenames : dir . files ".csv"
          foreach F in `filenames' {
          import delimited "`F'" , delimiter(tab) varnames(nonames) rowrange(6)
          local number : subinstr("`F'","....Session",,"",.)
          local number : subinstr("`number'",".csv","",.)
          save "KGC_05_Session_`number'.dta", replace
          ...
          }
          Where you will probably have to be a bit more careful than I have in pulling out the -number-, but you get the idea.

          hth,
          Jeph

          Hi Jeph
          It would be great to automate reading the CSV files - They are in the same directory so it should be possible. I´ve tried adding the full adresses and running your code, but nothing happens - I dont even get an error message. I cant figure out what I'm doing wrong. Can you help me?
          Also - Can you recommend somewhere where I can and learn about the commands "local", "foreach" and "subinstr"?


          The files I want to import have this addres:
          C:\Users\Sharsted\OneDrive\PhD\Validation\Data\Har sted Test\KGC6\Session1\Session102.csv
          Only the number "102" changes in the file names.
          In the code I have specified the directory where I would like the saved import files to be.


          Ive copied the do file into the directory where the CSV files are (C:\Users\Sharsted\OneDrive\PhD\Validation\Data\Ha rsted Test\KGC6\Session1\) and run the file from there.
          My code looks like this.

          Code:
          clear
          local filenames : dir . files ".csv"
          foreach F in `filenames' {
                 import delimited "`F'" , delimiter(tab) varnames(nonames) rowrange(6)
                 local number : subinstr("`F'","C:\Users\Sharsted\OneDrive\PhD\Validation\Rensede data\KGC 06\KGC_06_Session_",,"",.)
                 local number : subinstr("`number'",".csv","",.)
                 save "C:\Users\Sharsted\OneDrive\PhD\Validation\Rensede data\KGC 06\KGC_06_Session_`number'.dta"
                 capture log close
          log using "C:\Users\Sharsted\OneDrive\PhD\Validation\Rensede data\KGC 06\KGC_06_Session_`number'log.txt", text replace
          cd "C:\Users\Sharsted\OneDrive\PhD\Validation\Rensede data\KGC 06"
          use KGC_06_Session_`number'.dta, clear
          *All my renaming and droping of variables runs
          save "KGC_06_Session_`number'_cleaned ", replace
          clear
          log close

          Comment

          Working...
          X