Announcement

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

  • Trying to label multiple variables using loop

    Hi all,

    I am trying to label multiple variables at once using loop.

    For instance, variables q2_4_1_1,...,q2_4_1_6 should all be labelled as "Cows"
    On the other hand, variables q2_4_2_1,...,q2_4_2_6 should all be labelled as "Chicken"

    I have tried the following:

    Code:
    local x=1/6
    local y=1/3
    
    local localvarlabels "Cows" "Chicken" "Goats" "Others"
    
    foreach v of varlist q2_4_`x"_`y" q2_4_`x"_99 {
    label var `v' "`localvarlabels`"
    }
    Can someone please tell me what am I doing wrong here?

    Thanks in advance.
    Last edited by Alina Faruk; 09 Sep 2019, 11:06.

  • #2
    I think this will start you in a helpful direction.
    Code:
    . local animals Cows Chicken Goats Others
    
    . local a 0
    
    . foreach animal of local animals {
      2.     local a = `a' + 1
      3.     forvalues v=1/6 {
      4.         label variable q2_4_`a'_`v' "`animal'"
      5.     }
      6. }
    
    . describe
    
    Contains data
      obs:            42                          
     vars:            24                          
    ------------------------------------------------------------------------------------------------
                  storage   display    value
    variable name   type    format     label      variable label
    ------------------------------------------------------------------------------------------------
    q2_4_1_1        float   %9.0g                 Cows
    q2_4_1_2        float   %9.0g                 Cows
    q2_4_1_3        float   %9.0g                 Cows
    q2_4_1_4        float   %9.0g                 Cows
    q2_4_1_5        float   %9.0g                 Cows
    q2_4_1_6        float   %9.0g                 Cows
    q2_4_2_1        float   %9.0g                 Chicken
    q2_4_2_2        float   %9.0g                 Chicken
    q2_4_2_3        float   %9.0g                 Chicken
    q2_4_2_4        float   %9.0g                 Chicken
    q2_4_2_5        float   %9.0g                 Chicken
    q2_4_2_6        float   %9.0g                 Chicken
    q2_4_3_1        float   %9.0g                 Goats
    q2_4_3_2        float   %9.0g                 Goats
    q2_4_3_3        float   %9.0g                 Goats
    q2_4_3_4        float   %9.0g                 Goats
    q2_4_3_5        float   %9.0g                 Goats
    q2_4_3_6        float   %9.0g                 Goats
    q2_4_4_1        float   %9.0g                 Others
    q2_4_4_2        float   %9.0g                 Others
    q2_4_4_3        float   %9.0g                 Others
    q2_4_4_4        float   %9.0g                 Others
    q2_4_4_5        float   %9.0g                 Others
    q2_4_4_6        float   %9.0g                 Others
    ------------------------------------------------------------------------------------------------
    Sorted by: 
         Note: Dataset has changed since last saved.
    
    .

    Comment


    • #3
      William gives good advice. For convenience, see also elabel (SSC), which implements one of the loops.

      Depending on whether the wildcards match only the sought variables, you could

      Code:
      elabel variable (q2_4_1_*) ("Cows")
      ...
      elabel variable (q2_4_4_*) ("Others")
      Best
      Daniel
      Last edited by daniel klein; 09 Sep 2019, 12:51.

      Comment


      • #4
        Originally posted by William Lisowski View Post
        I think this will start you in a helpful direction.
        Code:
        . local animals Cows Chicken Goats Others
        
        . local a 0
        
        . foreach animal of local animals {
        2. local a = `a' + 1
        3. forvalues v=1/6 {
        4. label variable q2_4_`a'_`v' "`animal'"
        5. }
        6. }
        
        . describe
        
        Contains data
        obs: 42
        vars: 24
        ------------------------------------------------------------------------------------------------
        storage display value
        variable name type format label variable label
        ------------------------------------------------------------------------------------------------
        q2_4_1_1 float %9.0g Cows
        q2_4_1_2 float %9.0g Cows
        q2_4_1_3 float %9.0g Cows
        q2_4_1_4 float %9.0g Cows
        q2_4_1_5 float %9.0g Cows
        q2_4_1_6 float %9.0g Cows
        q2_4_2_1 float %9.0g Chicken
        q2_4_2_2 float %9.0g Chicken
        q2_4_2_3 float %9.0g Chicken
        q2_4_2_4 float %9.0g Chicken
        q2_4_2_5 float %9.0g Chicken
        q2_4_2_6 float %9.0g Chicken
        q2_4_3_1 float %9.0g Goats
        q2_4_3_2 float %9.0g Goats
        q2_4_3_3 float %9.0g Goats
        q2_4_3_4 float %9.0g Goats
        q2_4_3_5 float %9.0g Goats
        q2_4_3_6 float %9.0g Goats
        q2_4_4_1 float %9.0g Others
        q2_4_4_2 float %9.0g Others
        q2_4_4_3 float %9.0g Others
        q2_4_4_4 float %9.0g Others
        q2_4_4_5 float %9.0g Others
        q2_4_4_6 float %9.0g Others
        ------------------------------------------------------------------------------------------------
        Sorted by:
        Note: Dataset has changed since last saved.
        
        .
        Thank you very much!!! I am still new to loops so this really helped a lot.

        Comment


        • #5
          Originally posted by daniel klein View Post
          William gives good advice. For convenience, see also elabel (SSC), which implements one of the loops.

          Depending on whether the wildcards match only the sought variables, you could

          Code:
          elabel variable (q2_4_1_*) ("Cows")
          ...
          elabel variable (q2_4_4_*) ("Others")
          Best
          Daniel
          This is indeed very handy. Thanks so much for mentioning!

          Comment


          • #6
            Originally posted by daniel klein View Post
            William gives good advice. For convenience, see also elabel (SSC), which implements one of the loops.

            Depending on whether the wildcards match only the sought variables, you could

            Code:
            elabel variable (q2_4_1_*) ("Cows")
            ...
            elabel variable (q2_4_4_*) ("Others")
            Best
            Daniel
            Hi Daniel,

            The elabel command appears to make life a lot easier. Do you have any idea how to execute something like the following:

            elabel variable (q2_4_*_1) ("2.4.* Cows") where the asterisk in the label also takes on incremental values? I tried the following two options:

            Code:
            local i=1/6
            elabel variable (q2_4_*_1) ("2.4.`i' Cows")
            and
            Code:
            local i 0
            local i=`i'+1
            elabel variable (q2_4_*_1) ("2.4.`i' Cows")
            But none of these worked as desired.
            Last edited by Alina Faruk; 11 Sep 2019, 16:43.

            Comment


            • #7
              Alina

              I think you are confused about some fundamentals in Stata's syntax. I will try to explain.

              You repeatedly propose to code

              Code:
              local i=1/6
              What this code does is divide 1 by 6 and store the result into local macro i. See?

              Code:
              . local i=1/6
              
              . display "`i'"
              .1666666666666667
              Therefore, when you code

              Code:
              local i=1/6
              elabel variable (q2_4_*_1)  ("2.4.`i' Cows")
              Stata (and elabel) sees

              Code:
              elabel variable (q2_4_*_1)  ("2.4..1666666666666667 Cows")
              This is most likely not what you want here. You probably want something along the lines

              Code:
              forvalues i = 1/6 {
                  ...
              }
              where the code defines (and through the loop re-defines) local macro i but where code following forvalues is interpreted as numlist; here 1/6 is interpreted to mean the integer sequence 1, 2, ..., 6.

              Also, understand that is forvalues that increments the local macro i. When you say

              where the asterisk in the label also takes on incremental values?
              you seem to be under the impression that the wildcard character * picks up values. That is not so. The asterisk picks up zero or more characters; that is, any character. When you code

              Code:
              elabel variable (q2_4_*_1) ...
              elabel merely expands the variable list q2_4_*_1. The expanded variable list could include variables such as q2_4_1_1 and q2_4_2_1; it could also include variables such as q2_4_SOMETHINGHERE_1 and q2_4__1 if those variables were present in the dataset.

              Perhaps my statement in #3 that

              elabel [...] implements one of the loops.
              was slightly misleading. I was to say that elabel lets you assign variable labels to a list of variables.

              In your original post, there were only 4 fixed labels: Cows, Chicken, Goats, Others for 4 groups of variables. In that situation, you would assign the same label to a list of variables; elabel lets you do this without setting up a loop. In your recent post, it appears as if there are as many labels as variables. elabel could still handle such parallel lists if the labels were already stored in a list. Because they are not, I think you are better off just setting up the loop yourself. If I understand you correctly, you want something like

              Code:
              forvalues i = 1/6 {
                  label variable q2_4_`i'_1 "4.2.`i' Cows"
              }
              Best
              Daniel
              Last edited by daniel klein; 12 Sep 2019, 05:53.

              Comment

              Working...
              X