Announcement

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

  • how to rename variable name through all .dta files using loop function?

    So I have 20 .dta files on test score of 9-12 graders during years 2008-2012 (4*5=20). I want to rename v1 and v2 to id and score. Followings are what I wrote. after running it v1 is erased and instead i see <stata_dta><header><release>118</release><byteorder>LSF</byteorder><K>, v2 is completely gone, and program gives "v2 not found" error. I check data before running and all variables are there. I am completely new to STATA, do not anything about coding, so may have made basic mistakes.

    local myfilelist : dir . files"*.dta"
    foreach file of local myfilelist {
    drop _all
    insheet using `file'
    rename (v1 v2) (id score)
    save "`outfile'", replace
    }

    P.S. these .dta files were produced by following code:
    local myfilelist : dir . files"*.csv"
    foreach file of local myfilelist {
    drop _all
    insheet using `file'
    local outfile = subinstr("`file'",".csv","",.)
    save "`outfile'", replace
    }

  • #2
    Welcome to Statalist, and to Stata.

    I'm sympathetic to you as a new user of Stata - it's a lot to absorb. And even worse if perhaps you are under pressure to produce some output quickly. Nevertheless, I'd like to encourage you to take a step back from your immediate tasks.

    When I began using Stata in a serious way, I started, as have others here, by reading my way through the Getting Started with Stata manual relevant to my setup. Chapter 18 then gives suggested further reading, much of which is in the Stata User's Guide, and I worked my way through much of that reading as well. There are a lot of examples to copy and paste into Stata's do-file editor to run yourself, and better yet, to experiment with changing the options to see how the results change.

    All of these manuals are included as PDFs in the Stata installation (since version 11) and are accessible from within Stata - for example, through the PDF Documentation section of Stata's Help menu. The objective in doing the reading was not so much to master Stata as to be sure I'd become familiar with a wide variety of important basic techniques, so that when the time came that I needed them, I might recall their existence, if not the full syntax, and know how to find out more about them in the help files and PDF manuals.

    Stata supplies exceptionally good documentation that amply repays the time spent studying it - there's just a lot of it. The path I followed surfaces the things you need to know to get started in a hurry and to work effectively.

    Your problem is that insheet reads your .csv files into memory, but it is the use command that would read your .dta files into memory.

    Comment


    • #3
      insheet is used to read in text files not for reading in Stata data files. Try

      Code:
      local myfilelist : dir . files"*.dta"
      foreach f in `myfilelist' {
          use `f', clear
          rename (mpg weight) (m w)
          save, replace
      }
      Also, from the insheet helpfle:
      insheet has been superseded by import delimited. insheet continues to work but, as of Stata 13, is no longer an
      official part of Stata. This is the original help file, which we will no longer update, so some links may no
      longer work.

      Comment

      Working...
      X