Announcement

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

  • How to loop variables' labels in stata


    Hello, I am using Stata 16.0, I have a dataset with many variables concerning election results.
    I have some variables that list the amount of votes certain parties got, and some variables
    that list the share of the votes that such parties got in the election.
    In this short example I have two parties, then for each party I have a variable
    called S_percent_NAMEPARTY (share of votes), and a variable called S_Votes_NAMEPARTY (total amount of votes), so that
    I have 2*2=4 variables.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str34 comune_oggi float S_percent_M5S double S_Votes_M5S float S_percent_FDI double S_Votes_FDI
    "Milano" .14763483 99395 .01660604 11180
    end
    The toydataset looks like this :
    Code:
    . list
    
         +------------------------------------------------------+
         | comune~i   S_per~5S   S_Vot~5S   S_perc~I   S_Vote~I |
         |------------------------------------------------------|
      1. |   Milano   .1476348      99395    .016606      11180 |
         +------------------------------------------------------+
    Since in the original dataset I have more parties (and variables) I would like
    to label these variables with a loop which should work in this way :
    My objective is to generate a loop that works in this way :

    Code:
    label variable S_percent_NAMEPARTY "%  NAMEPARTY"
    label variable S_votes_NAMEPARTY "NAMEPARTY TOT VOTES"


    This was my attempt :

    Code:
    . foreach x in  M5S FDI{
      2. label variabel S_Votes_`x'  "`x' tot votes"
      3. label variabe  S_Votes_`x'  " % `x'"
      4. }
    invalid syntax
    r(198);

  • #2
    Code:
    foreach x in  M5S FDI{
         label variable S_Votes_`x'  "`x' tot votes"
         label variable  S_percent_`x'  " % `x'"
    }
    Your original code had two different misspellings of "variable" Also, one of the commands should relabel S_percent_`x', whereas you relabeled S_Votes_`x' twice (with different labels)

    Added: You don't actually need to write out the complete word variable in the -label- command. You can shorten it to just var, or anything between var and variable. But when you have an actual misspelling, Stata can't recognize that.

    Comment


    • #3
      Hi Clyde, thank you for the reply.
      Now it's working.
      I am sorry for this silly thread, I guess it's time for me to take break.

      Thanks again

      Comment


      • #4
        I am sorry for this silly thread
        No need to apologize. We all make mistakes like that. And both of the misspellings look, at first glance, like the correct word variable. And in natural reading, or brains tend to perceive what we think should be there, even when it isn't quite so. So unless you scrutinize every character, it's easy to miss the problem. Yes, taking a break can refresh and be helpful.

        When I first looked casually at your code, I didn't spot the problem either. What finally tipped me off was noticing that in the second command the var... word was 1 character shorter than in the first--so I figured at least one of them had to be wrong. Then I looked at each one character by character and saw they were both wrong. But it's not something that jumps out at you.

        Comment

        Working...
        X