Announcement

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

  • Create a label including prior labels (in a loop)

    Dear all,

    I am new to using Stata and the forum, so I hope it is okay to post this question here. I couldn't find an answer in my prior research.


    I generated new variables using a loop and want to create a label for each of these variables within the loop. My problem now is that I have already labeled the old variables used to create the new variables and want to use their labels in the new labels.

    Here is what I have done:

    foreach control in spv_s_u3y populationDensity foreigners_share ue_rate GDP_perInhabitant{
    forvalues y =2011(1)2019 {
    sum(`control'`y')
    gen d_`control'`y' = ((`control'`y'-r(mean))/r(sd))
    label var d_`control'`y' "`control' in `y' (in SD)"
    }
    }

    E.g. the variable spv_s_u3y is labeled "Shares of children supervised under 3 years in `y'".

    The label for the new variable d_spv_s_u3y is now not using the label of spv_s_u3y but the variable name spv_s_u3y.

    Is it possible to use the already defined variable label in the label for the new variables without labeling each individually?

    Thanks in advance,
    Marla Feldwisch

  • #2
    Code:
    foreach control in spv_s_u3y populationDensity foreigners_share ue_rate GDP_perInhabitant{
        forvalues y =2011(1)2019 {
            sum(`control'`y')
            gen d_`control'`y' = ((`control'`y'-r(mean))/r(sd))
            label var d_`control'`y' "`: var label `control'' in `y' (in SD)"
        }
    }
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Thank you! It works perfectly and helps me a lot

      Comment

      Working...
      X