Announcement

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

  • Change Variable name to Label name

    Dear All,

    Could someone explain how I can change all the Variable names by my current Label names?
    For example, Variable B with Label 2670 should be named 2670.
    Variable C should be names 7372 and so on..

    Kind regards,
    Emiel Brak

  • #2
    That is not possible, as variable names cannot start with numbers. Also from the screenshot (note that an example would have been better) there seem to be variables without labels. A successful approach depends much on such details, so please say (or better show) more about the nature of your dataset. You may find dataex (SSC) useful.

    The basic loop you want is something like

    Code:
    foreach var of varlist * {
        local label : variable label `var'
        if ("`label'" != "") {
            local oldnames `oldnames' `var'
            local newnames `newnames' _`label'
        }
    }
    
    rename (`oldnames')(`newnames')
    assuming (i) the variable labels are unique (i.e. each label appears at most once), (ii) the labels do not have embedded spaces and/or other characters that are not valid Stata (variable) names and (iii) the labels are at most 31 characters in length. Other assumptions might be needed for the approach to work.

    Best
    Daniel
    Last edited by daniel klein; 12 Oct 2015, 09:41.

    Comment


    • #3
      Dear Daniel,
      Thanks for your reply. In the first column of my Stata-file I have dates on a daily basis for 25 years and on the first row I have SIC-codes, each SIC-code (1800x) represents the industry a company is active in. So for 1800 companies I have stock prices, from which I later will calculate (abnormal) returns. Since some companies are active in the same industry they have the same SIC code. So coming back to your assumptions.
      I: variable labels are not unique
      2: Yes, no embedded spaces
      3: labels are at most 31 characters

      However, I need the labels to be SIC-codes (or related to that, yes they can start with a letter for example).
      Do you have any advice how to go further?

      Kind regards,
      Emiel

      Comment


      • #4
        Just a minor follow-on comment:

        Originally posted by daniel klein View Post
        That is not possible, as variable names cannot start with numbers. ...

        assuming (i) the variable labels are unique (i.e. each label appears at most once), (ii) the labels do not have embedded spaces and/or other characters that are not valid Stata (variable) names and (iii) the labels are at most 31 characters in length. Other assumptions might be needed for the approach to work.
        The -strtoname()- function should help address some of these problems (not (i)), while -subinstr- can help take care of removing extraneous (or all, as you prefer) instances of "_" resulting from -strtoname()- (or -regexs()- can be used for more flexible renaming -- e.g., placing "_1234" after "sic" to form "sic_1234").

        Code:
        . di strtoname("label to varname")
        label_to_varname
        
        . di strtoname("label to varname #2")
        label_to_varname__2
        
        . di strtoname("1234 abcd")
        _1234_abcd
        
        . di strtoname("1234 starts with a number and is too long for a varname")
        _1234_starts_with_a_number_and_i

        Comment


        • #5
          Emiel,

          The command renvarlab (available from SSC) does what you are asking for and allows for the issue that Daniel raises by doing what Brendan suggests.

          Regards,
          Joe

          Comment

          Working...
          X