Announcement

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

  • Insheet a file with all the variables in string format


    I have a file in .txt format but when I use the insheet command STATA do not recognize some variables as string. I do not want to lose information because some IDs have a zero leading ID code. Right now i´m editing the .txt file, but i want to know if there is an option to import the data as string for every variable

  • #2
    what version of Stata are you using? "Import delimited" has superseded "insheet," though I don't recall what version.

    If you do have Stata 13 or 14 (or possibly earlier),
    Code:
    import delimited using filename.csv, stringcols(_all)
    should do the trick.

    Comment


    • #3
      Originally posted by ben earnhart View Post
      what version of Stata are you using? "Import delimited" has superseded "insheet," though I don't recall what version.

      If you do have Stata 13 or 14 (or possibly earlier),
      Code:
      import delimited using filename.csv, stringcols(_all)
      should do the trick.
      Thank you very much. I finally solve that problem.

      Comment


      • #4
        Note also that a leading 0 can be restored fairly easily.

        Code:
        clear
        input float id
        12345678
        98765432
        end
        format id %9.0f 
        gen sid1 = string(id, "%09.0f")
        gen sid2 = string(id, "%010.0f")
        list 
        
             +-----------------------------------+
             |       id        sid1         sid2 |
             |-----------------------------------|
          1. | 12345678   012345678   0012345678 |
          2. | 98765432   098765432   0098765432 |
             +-----------------------------------+

        Comment


        • #5
          Nick, that assumes that all codes have exactly the same length. For those who don't have access to a more modern version of Stata, if the text file includes variable names, you can force insheet to read all variables as string if you use the nonames option. You then loop to rename variables once the data is in memory. For example:

          Code:
          clear
          input str4 code
          0012
          0123
          1234
          05
          56
          end
          gen id = _n
          outsheet using "test_data.txt", replace
          
          clear
          insheet using "test_data.txt", nonames
          
          foreach v of varlist * {
              local vname = `v'[1]
              local vname = strtoname(`"`vname'"')
              rename `v' `vname'
          }
          
          drop in 1
          destring id, replace
          If the text file has no variable names, then you make a duplicate of the text file and use your favorite text editor to insert a variable name line at the top of the file and proceed as described above.
          Last edited by Robert Picard; 16 May 2016, 09:35. Reason: added a strtoname() call to make sure that the value in the first observation forms a valid Stata variable name

          Comment


          • #6
            Originally posted by Nick Cox View Post
            Note also that a leading 0 can be restored fairly easily.

            Code:
            clear
            input float id
            12345678
            98765432
            end
            format id %9.0f
            gen sid1 = string(id, "%09.0f")
            gen sid2 = string(id, "%010.0f")
            list
            
            +-----------------------------------+
            | id sid1 sid2 |
            |-----------------------------------|
            1. | 12345678 012345678 0012345678 |
            2. | 98765432 098765432 0098765432 |
            +-----------------------------------+
            thank you nick

            Comment

            Working...
            X