Announcement

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

  • Converting multiple files´ variables to string

    I have several .dta files in a folder. For each file, how can I convert all the variables to string? That is, I want to apply the following code to all files.
    Code:
    tostring *, replace
    Last edited by Budu Gulo; 20 Dec 2018, 12:10.

  • #2
    If you have previously obtained those files from Excel via my xls2dta command, as this recent thread implies, you can

    Code:
    xls2dta ... : import excel ...
    xls2dta : xeq tostring *, replace
    Best
    Daniel

    Comment


    • #3
      The following code is a start, assuming all of the files concerned are located in the current working directory, and all of the .dta files in that directory are to have this conversion done.

      Code:
      clear*
      
      local filenames: dir "." files "*.dta"
      
      foreach f of local filenames {
          use `"`f'"', clear
          tostring *, replace
          save `"`f'"', replace
      }
      It is not tested, and may contain errors. But more fundamentally, attempting a global -tostring- even in a single file is potentially problematic. If the numeric variables in the file are all integers, there shouldn't be a problem. But if some of them are not, -tostring- might refuse to convert them. -tostring- will not convert a variable to string if doing so would result in loss of information. This happens because non-integers can only be approximated in binary storage, so converting to a finite string could result in a number that, when converted back to numeric, produces a different result from the original. Here's a very simple example:

      Code:
      clear
      set obs 1
      gen x = 1/3
      list
      tostring x, replace
      foromat x %13.12f
      list
      Because 1/3 cannot be stored exactly, the number actually stored in x is not really 1/3, it is just a very close binary approximation, as the final -list- command shows. If that were converted to string, and the resulting string converted back to numeric, the original value would not necessarily be recreated. So -tostring- will not go along with that.

      Now, it is often the case that the imprecision involved here is too small to matter for the purposes at hand. If you are sure that is the case in your data sets, you can add the -force- option to the -tostring- command and you will get what you want. But you should carefully review all of the data sets before doing this--once you lose the information you cannot get it back.

      Added: Crossed with #2.

      Comment


      • #4
        Thanks daniel klein! I indeed obtained those files using xls2dta. Your code works perfectly.

        For learning's sake, suppose the files were not obtained using xls2dta,what approach will solve the problem?

        Comment


        • #5
          Many thanks Clyde Schechter. You are super fast! My question in #4 is now irrelevant.

          Comment

          Working...
          X