Announcement

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

  • Using a loop to rename variable label that has distinct symbols

    Hi Statalist community,

    I am using the voter.dta dataset as an example. I am using the tabulate command to create a series of dummy variables from a categorical variable called candidat. For each of the dummy variables I created, I want to revise the variable label so that it identifies each category of the original categorical variable candidat. When I use the tabulate command, the variable labels begin with candidat== but I want all the characters after the == symbols. It's not a problem for me to manually rename each variable label since this dataset is small but it is a pain when I have a categorical variable with many categories. I tried writing a loop below but could not get it to work. Any help would be appreciated.


    Code:
    clear all
    *Call in data
    webuse voter.dta
    *Create a dummy variable for each of three candidates
    tabulate candidat , gen (dum_cand)
    
    **This is what I am aiming to accomplish.
    *Rename variable labels
    label variable dum_cand1 "Clinton"
    label variable dum_cand2 "Bush"
    label variable dum_cand3 "Perot"
    
    *This is the loop that I tried to write but doesn't work.
    *Create a loop to label the variable
    foreach var of varlist dum_cand1-dum_cand3 {
        label variable `var' `=substr("`var'", "==")'
    }

  • #2
    Perhaps this will point you in a useful direction.
    Code:
    . clear all
    
    . *Call in data
    . webuse voter.dta
    (1992 U.S. presidential voters)
    
    . *Create a dummy variable for each of three candidates
    . tabulate candidat , gen (dum_cand)
    
      Candidate |
     voted for, |
           1992 |      Freq.     Percent        Cum.
    ------------+-----------------------------------
        Clinton |          5       33.33       33.33
           Bush |          5       33.33       66.67
          Perot |          5       33.33      100.00
    ------------+-----------------------------------
          Total |         15      100.00
    
    . describe dum_cand1-dum_cand3
    
    Variable      Storage   Display    Value
        name         type    format    label      Variable label
    ------------------------------------------------------------------------------------------------
    dum_cand1       byte    %8.0g                 candidat==Clinton
    dum_cand2       byte    %8.0g                 candidat==Bush
    dum_cand3       byte    %8.0g                 candidat==Perot
    
    . *Create a loop to label the variable
    . foreach var of varlist dum_cand1-dum_cand3 {
      2.     local lbl : variable label `var'
      3.     local lbl : subinstr local lbl "candidat==" ""
      4.     label variable `var' "`lbl'"
      5. }
    
    . describe dum_cand1-dum_cand3
    
    Variable      Storage   Display    Value
        name         type    format    label      Variable label
    ------------------------------------------------------------------------------------------------
    dum_cand1       byte    %8.0g                 Clinton
    dum_cand2       byte    %8.0g                 Bush
    dum_cand3       byte    %8.0g                 Perot

    Comment


    • #3
      @William Lisowski
      Thank you so much. This works like a charm. Is it possible for you to explain what you are doing in the loop? In particular, what does the following line do?

      Code:
       
        local lbl : subinstr local lbl "candidat==" ""

      I looked up the subinstr function which led me to this post below but I can't figure out what you did. Thanks.

      https://www.statalist.org/forums/for...instr-function


      Comment


      • #4
        Perhaps the problem is that you looked at documentation for Stata's subinstr() function, which is not directly used in the command you show.

        The two macro functions used in the following commands
        Code:
        local lbl : variable label `var'
        local lbl : subinstr local lbl "candidat==" ""
        are documented in the output of help macro.

        The first command creates the local macro lbl with the variable label for the variable `var' - for dum_cand1 this is "candidat==Clinton".

        The second command replaces the local macro lbl with the results of substituting the first occurrence of "candidat==" in the local macro lbl with "" - that is, replacing "candidat==Clinton" with "Clinton".

        Comment


        • #5
          @William Lisowski
          Thank you so much for the explanation. That makes sense.

          Comment

          Working...
          X