Announcement

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

  • Transform first observation with blanks into variable names

    Dear Statalist,

    I am currently using the -import command- to get .txt files into .dta files.
    Even tough I want in fact to read the first observations in the .txt files as variable names in Stata, the existence of repeated variables and variables with equal names in the .txt file prevents-me to load the data to Stata. The solution is therefore to do not take first observations as variable names and to, set the option [, varnames(1)] instead of [, varnames(nonames)].
    But then first observations contain blank spaces which do not allow me for an easy replacement as variable names that, by definition, can have no spaces.

    So, in my view I first have to eliminate blank spaces so that I can run a -foreach- program to rename variable with the contents of the first observation. But I don't know how I should proceed with this since the more usual character replacement works by variable and not by observation (for instance: replace var1=subinstr(var1,"`=char(xx)'","",.).

    I have also looked at how could I apply -forvalues- ( http://www.stata.com/statalist/archi.../msg01171.html ) and -foreach- ( http://www.stata.com/statalist/archi.../msg00825.html )
    examples as well as a renvars ( http://www.stata.com/statalist/archi.../msg01177.html ) but I am also not sure how to adapt these programs.


    Any help on this would be greatly appreciated,

    André Coelho



  • #2
    As I understand it, you want to loop over your variables, taking their new names from the first observation. That might fail if only because the first value could be blank. So, presumably everything is imported as string.

    Something like this might be what you seek:

    Code:
    local j = 1 
    foreach v of var * { 
       capture rename `v' `=`v'[1]' 
       if _rc { 
             rename `v' othervar`j' 
             local ++j 
       }
    }
    
    drop in 1 
    destring, replace

    Comment


    • #3
      Dear Nick,

      Thank you for your help. Yes you are right - all variables were imported as strings.

      I run your code and I got this (an extract with variables displayed in lines before and after your command).
      Before First Obs After
      v1 ID ID
      v2 Number of months othervar1
      v3 Number of employees othervar2
      v4 Fixed assets othervar3
      v5 Intangible fixed assets othervar4
      v6 Tangible fixed assets othervar5
      v7 Other fixed assets othervar6
      v8 Current assets othervar7
      v9 Stock Stock
      Indeed this is almost what I wanted. To be exactly what I am looking for, v2 would be renamed into "Numberofemployees" or "Number_of_employees" and so on with the remaining variables that contain spaces into the words. Is there any way of doing this?

      Thank you,

      André Coelho

      Comment


      • #4
        Got it.

        Code:
         
        local j = 1
        foreach v of var * {
            capture rename `v' `=strtoname(`v'[1])'
            if _rc {
               rename `v' othervar`j'
               local ++j
           }
        }
        drop in 1
        destring, replace

        Comment


        • #5
          Exactly. Thank you so much!

          André

          Comment

          Working...
          X