Announcement

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

  • How to loop labels?

    Hello Stata users!

    This is one of my first post and I basically am looking for an easier way to code labels.

    So I have a data base that has the same information for different animals.

    So our list of animals is: cow, bulls, chickens, buffalos, and sheep.

    We have information about each animal: number of (animal) farmer own, number of (animal) bought, number of (animal) sold, and number of (animal) died.
    The variables are coded the following way :
    First by type of information so for example, number of (animal) owned is coded as q1, bought is q2, etc.
    Each animal is coded as a1 (cows), a2 (bulls) etc.

    So our variable list is q1_a1; q1_a2;….. q2;_a1 etc.
    Where q1_a1 is number of cows a farmer owns, q1_a2 number of bulls a farmer owns, q2_a1 number of cow that were bought. I have a problem with the labeling:

    I am now labeling each of them and I am doing the following:

    Code:
    label variable q1_a1 “Q1.A1 Number of cows owned by Farmer”
    label variable q1_a2 “Q1.A2 Number of bulls owned by Farmer”
    This seems like a waste of time… I know how to use forvalues to change the labels such as:

    Code:
    forvalues x=1 (1) 5 {
    label variable q1_a`x' "Q1.A `x' Number of (animals) owned"
    }

    But in this case where (animals) is, I would like stata to put the animal. Such that it looks like "Q1.A1 Number of cows owned" in the label. Is there a way to do a loop so that when a1 appears it inserts as "cow"? I hope this is clear!! Thank you so much!

  • #2
    Welcome to Statalist.

    Perhaps the ideas in the example code will start you in a useful diirection.
    Code:
    describe, simple
    
    local animals cows bulls
    local types owned bought
    
    local x = 0
    foreach animal of local animals {
        local x = `++x'
        local y = 0
        foreach type of local types {
            local y = `++y'
            label variable q`y'_a`x' "Q`y'.A`x' Number of `animal' `type'"
        }
    }
    
    describe
    Code:
    . describe, simple
    id     q1_a1  q2_a1  q1_a2  q2_a2
    
    . 
    . local animals cows bulls
    
    . local types owned bought
    
    . 
    . local x = 0
    
    . foreach animal of local animals {
      2.     local x = `++x'
      3.     local y = 0
      4.     foreach type of local types {
      5.         local y = `++y'
      6.         label variable q`y'_a`x' "Q`y'.A`x' Number of `animal' `type'"
      7.     }
      8. }
    
    . 
    . describe
    
    Contains data
      obs:             1                          
     vars:             5                          
     size:            20                          
    ---------------------------------------------------------------------------
                  storage   display    value
    variable name   type    format     label      variable label
    ---------------------------------------------------------------------------
    id              float   %9.0g                 
    q1_a1           float   %9.0g                 Q1.A1 Number of cows owned
    q2_a1           float   %9.0g                 Q2.A1 Number of cows bought
    q1_a2           float   %9.0g                 Q1.A2 Number of bulls owned
    q2_a2           float   %9.0g                 Q2.A2 Number of bulls bought
    ---------------------------------------------------------------------------
    Sorted by: 
         Note: Dataset has changed since last saved.

    Comment


    • #3
      Looping over parallel lists is an important concept to learn. It is also the preferred solution to this problem. However, I would like to show a way of doing this with elabel (SSC).

      Code:
      // create variables
      clear
      foreach x in q1_a1  q2_a1  q1_a2  q2_a2 {
          generate `x' = 42
      }
      
      // label the variables
      elabel variable (q1_*) ("owned")
      elabel variable (q2_*) ("bought")
      elabel variable (*_a1) (= "Number of cows " + @)
      elabel variable (*_a2) (= "Number of bulls " + @)
      
      describe
      yields

      Code:
      . describe
      
      Contains data
        obs:             0                          
       vars:             4                          
       size:             0 (100.0% of memory free)
      -------------------------------------------------------------------------------------------------------------------------------------------------------------------
                    storage  display     value
      variable name   type   format      label      variable label
      -------------------------------------------------------------------------------------------------------------------------------------------------------------------
      q1_a1           float  %9.0g                  Number of cows owned
      q2_a1           float  %9.0g                  Number of cows bought
      q1_a2           float  %9.0g                  Number of bulls owned
      q2_a2           float  %9.0g                  Number of bulls bought
      -------------------------------------------------------------------------------------------------------------------------------------------------------------------
      Sorted by:  
           Note:  dataset has changed since last saved
      Best
      Daniel
      Last edited by daniel klein; 11 Jul 2019, 23:29.

      Comment


      • #4

        In #2 the increment of locals is written
        Code:
        local x = `++x'
        To increment local x use
        Code:
        local ++x
        equivalent to typing
        Code:
        local x = `x' + 1
        Ref.:
        Code:
        help macro
        and PDF manual [P] Macro > Macro expansion operators and function
        Last edited by Bjarte Aagnes; 12 Jul 2019, 07:17.

        Comment


        • #5
          Note though that while code in post #2 increments the local inelegantly it does still produce the correct result.
          Code:
          . macro list _x _y _z
          _x:             1
          _y:             1
          _z:             1
          
          . local x = `++x'
          
          . local ++y
          
          . local z = `z' + 1
          
          . macro list _x _y _z
          _x:             2
          _y:             2
          _z:             2

          Comment


          • #6
            Thank you guys so much!! Both for the code and explaining what `++x' did! I ended up using Williams as it was the easiest to grasp for me... but will look at daniel's in the future!

            Comment


            • #7
              Dear all:

              I have a new question related to the subject: What if what I want to label has spaces. From the example above we would have a category that is "milk cows" and another that is "meat cows".

              I had tried doing the following:

              Code:
              local types `" "Milk Cows" "Meat Cows" "'
              foreach i of numlist 1/2 {
                  foreach type of local types{
                  label variable q1_a`i' "Q1.`i' Number of "`type'" owned?"
              
              }
              }
              But this doesn't seem to work and I get the following message: invalid syntax.

              I believe its because its reading the quotations before `type' as the end of the label quotations.

              Do you guys have any advice?

              Thank you!!

              Comment


              • #8
                I think the following does what you want.
                Code:
                local types `" "Milk Cows" "Meat Cows" "'
                foreach i of numlist 1/2 {
                    local type : word `i' of `types'
                    label variable q1_a`i' "Q1.`i' Number of `type' owned?"
                }
                describe
                Code:
                --------------------------------------------------------------------------------
                              storage   display    value
                variable name   type    format     label      variable label
                --------------------------------------------------------------------------------
                q1_a1           float   %9.0g                 Q1.1 Number of Milk Cows owned?
                q1_a2           float   %9.0g                 Q1.2 Number of Meat Cows owned?
                --------------------------------------------------------------------------------

                Comment


                • #9
                  It worked perfectly! Thanks William!!!

                  Comment

                  Working...
                  X