Announcement

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

  • Variable names are not var1, var2, var3 ...., but A, B, C .......; how to make them var1, var2, var3

    Hi,

    I am importing some raw data file into Stata and I want to put the first observation into variable names; the problem I have is the variable names are A, B, C etc.
    In this case I can't make them in the loops such as :

    "
    forvalues j = 1/43 {
    label variable var`j' `=var`j'[1]'
    rename var`j' `=var`j'[1]'
    }
    "

    How can I come over this issue? or are there other ways to put variable names such as A, B, C et.c in a loop like above?

    Appreciate your help!
    Thanks

  • #2
    Alireza: Perhaps try something like this:
    Code:
    rename A-Z var#, addnumber

    Comment


    • #3
      Well, it sounds like you have imported a spreadsheet, and done so in a way that leaves the actual variable names you want in the first observation rather than as the variable names. One option, if the source is Excel, is to go back and re-import the spreadsheet using -import excel- and specify the -firstrow- option. Then the content of the first row will come in as the variable names in the first place.

      If that's not the original source (or you don't have the original Excel file and just have this), if A B C, etc. are all of the variables, then you could do it this way:

      Code:
      foreach v of varlist _all {
          label var `v' `"`=`v'[1]'"'
          rename `v' `=`v'[1]'
      }

      Comment


      • #4
        I agree with Clyde's advice that going back to the original is likely to be the best strategy.

        What follows is about the second-best strategy.

        I'd advise that names like var1 var2 var3 are surely no more informative than A B C. They are worse as requiring more typing! I wouldn't even want to memorize what each of var1 var2 var3 contains, let alone do that for more variables.

        So, I'd follow Clyde's advice with the extra principles:

        1. There is no guarantee in general that whatever is in the first observation is a legal variable name. Using capture does no harm here.

        2. Once you have copied the information from the first observation, you should drop it.

        3. If metadata got read as the first observation, it's all too likely that you now need destring.

        When cleaning up student messes, I often do something more like

        Code:
        foreach v of varlist _all {
            label var `v' `"`=`v'[1]'"'
            capture rename `v' `=`v'[1]'
        }
        
        drop in 1
        destring, replace
        Last edited by Nick Cox; 09 Apr 2017, 03:53.

        Comment


        • #5
          Originally posted by John Mullahy View Post
          Alireza: Perhaps try something like this:
          Code:
          rename A-Z var#, addnumber
          Greatly worked. Thanks

          Comment


          • #6
            Originally posted by Clyde Schechter View Post
            Well, it sounds like you have imported a spreadsheet, and done so in a way that leaves the actual variable names you want in the first observation rather than as the variable names. One option, if the source is Excel, is to go back and re-import the spreadsheet using -import excel- and specify the -firstrow- option. Then the content of the first row will come in as the variable names in the first place.

            If that's not the original source (or you don't have the original Excel file and just have this), if A B C, etc. are all of the variables, then you could do it this way:

            Code:
            foreach v of varlist _all {
            label var `v' `"`=`v'[1]'"'
            rename `v' `=`v'[1]'
            }
            The case is actually more complicated than that. The source data I am using has several rows that need to be dropped first (because of some instrument information in the first rows). I used "firstrow" option few times before when my raw data starts from the second row and the first row is specific to the name of variables, but in the current case, it does not work.

            Comment


            • #7
              #6 We don't know that if you don't tell us. More positively, import excel allows a cellrange() option.

              When I do this with students, this is one of the few cases where I use a dialog box, which allows a view of the top of the file as the command sees it.

              Comment

              Working...
              X