Announcement

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

  • Replacing Prefix of Variable Labels

    Hello all,
    I just reshaped data from long to wide and now have four sets of about 15 variables with the different notations I used in reshaping as part of the variable name and label. I was able to rename the variables in batches, but haven't been able to find a simple way to relabel just the prefix of the variable label.

    For example, one variable is labeled "111 countycode", another is "111 testyear". I used 111 to represent 11th Grade ELA testing when reshaping. I'd like to replace all of the 111's with "11th ELA" and leave the suffixes (e.g., countycode, testyear) as they are. I've played around with a few loops but haven't been able to figure out how to do this correctly.

    I appreciate any help you can offer!

  • #2
    This can be done with community-contributed code -- e.g. daniel klein has a versatile command for label stuff.

    Here is a solution entirely with official commands.

    Code:
    * test dataset 
    clear 
    set obs 1 
    foreach v in frog toad newt { 
        gen `v' = 42
    }
    
    label var frog "111 countycode" 
    label var toad "111 testyear"
    label var newt "nowt to do with owt"
    
    d 
    
    * start here 
    ds, has(varlabel "111*")
    
    foreach v in `r(varlist)' { 
        local label : var label `v' 
        if strpos("`label'", "111") == 1 { 
            local label : subinstr local label "111" "11th ELA"
            label var `v' "`label'" 
        }
    }
    
    d 
    
    
    Contains data
     Observations:             1                  
        Variables:             3                  
    ------------------------------------------------------------------------------------------------
    Variable      Storage   Display    Value
        name         type    format    label      Variable label
    ------------------------------------------------------------------------------------------------
    frog            float   %9.0g                 11th ELA countycode
    toad            float   %9.0g                 11th ELA testyear
    newt            float   %9.0g                 nowt to do with owt
    ------------------------------------------------------------------------------------------------
    Sorted by: 
         Note: Dataset has changed since last saved.

    Comment


    • #3
      EDIT:

      The code in #2 does not really need the call to ds.

      Code:
      foreach v of varlist * { 
          local label : var label `v' 
          if strpos("`label'", "111") == 1 { 
              local label : subinstr local label "111" "11th ELA"
              label var `v' "`label'" 
          }
      }

      Here is a solution using elabel (SSC):

      Code:
      elabel variable (*) (= subinstr(@, "111", "11th ELA", .))
      Last edited by daniel klein; 07 Oct 2021, 09:28.

      Comment


      • #4
        Wonderful, thank you all so much!
        I just downloaded elabel and was able to rename all of the variable labels in about 2 minutes

        Comment

        Working...
        X