Announcement

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

  • looping for labeling variables

    Hello Stata users,
    I get error as " invalid syntax r(198); end of do-file r(198);" when I run this:
    Code:
    forvalues event=1/8{
          forvalues year = 2023(-1)2013 {
          gen eventNo= usubstr("q23a`event'_`year'",-6,1)
          label variable q23a`event'_`year' "Q23A`event'.Year of Drought:`year'" if eventNo==1
          drop eventNo
          }
    }

    I will appreciate the help you offer.

  • #2
    The syntax

    Code:
    if eventNo==1 
    does not belong at all in any use of the label command. Perhaps this is what you want:
    Code:
      
    forvalues event = 1/8 {    
        forvalues year = 2023(-1)2013 {        
            if usubstr("q23a`event'_`year'",-6,1) == "1" {              
                label variable q23a`event'_`year' "Q23A`event'.Year of Drought:`year'"          
            }    
       }
    }

    Comment


    • #3
      Originally posted by Nick Cox View Post
      The syntax

      Code:
      if eventNo==1 
      does not belong at all in any use of the label command. Perhaps this is what you want:
      Code:
      forvalues event = 1/8 {
      forvalues year = 2023(-1)2013 {
      if usubstr("q23a`event'_`year'",-6,1) == "1" {
      label variable q23a`event'_`year' "Q23A`event'.Year of Drought:`year'"
      }
      }
      }
      My sincere thanks, Nick. This is what I need. But I apologize for not initially laying out the entire thought in the codes. I need not only drought, but also other 7 events that need to be labeled. I could not adapt you approach for this Full idea in syntax:

      Code:
      forvalues event=1/8{
            forvalues year = 2023(-1)2013 {
            di `event' 
            di `year'
            gen eventNo= usubstr("q23a`event'_`year'",-6,1)
            label variable q23a`event'_`year' "Q23A`event'.Year of Drought:`year'" if eventNo==1
            label variable q23a`event'_`year' "Q23A`event'.Year of Roo much rain: `year'" if eventNo==2
            label variable q23a`event'_`year' "Q23A`event'.Year of Floods: `year'" if eventNo==3
            label variable q23a`event'_`year' "Q23A`event'.Year of Landslides: `year'" if eventNo==4
            label variable q23a`event'_`year' "Q23A`event'.Year of Very cold winter: `year'" if eventNo==5
            label variable q23a`event'_`year' "Q23A`event'.Year of Frosts: `year'" if eventNo==6
            label variable q23a`event'_`year' "Q23A`event'.Year of Hail: `year'" if eventNo==7
            label variable q23a`event'_`year' "Q23A`event'.Year of Other natural disasters: `year'" if eventNo==8
            drop eventNo
            }
      }

      Comment


      • #4
        This isn't the shortest possible code but it may be clearer than that.

        In long value labels, text that discriminates should come first. From most points of view the generic text

        Code:
        Q23A`event'.Year of
        is not interesting or useful. Who will care that the data are answers to Q23A?

        I've not checked that your value labels are within 32 characters. I've corrected "Roo" to "Too", a QWERTY error perhaps.

        Code:
        local lbl1 Drought 
        local lbl2 Too much rain 
        local lbl3 Floods
        local lbl4 Landslides
        local lbl5 Very cold winter 
        local lbl6 Frosts 
        local lbl7 Hail 
        local lbl8 Other natural disasters 
        
        forvalues event = 1/8 {
            forvalues year = 2023(-1)2013 {
                di `event' " " `year'
                label variable q23a`event'_`year' "Q23A`event'.Year of `lbl`event'':`year'" 
            }
        }

        Comment


        • #5
          Originally posted by Nick Cox View Post
          This isn't the shortest possible code but it may be clearer than that.

          In long value labels, text that discriminates should come first. From most points of view the generic text

          Code:
          Q23A`event'.Year of
          is not interesting or useful. Who will care that the data are answers to Q23A?

          I've not checked that your value labels are within 32 characters. I've corrected "Roo" to "Too", a QWERTY error perhaps.

          Code:
          local lbl1 Drought
          local lbl2 Too much rain
          local lbl3 Floods
          local lbl4 Landslides
          local lbl5 Very cold winter
          local lbl6 Frosts
          local lbl7 Hail
          local lbl8 Other natural disasters
          
          forvalues event = 1/8 {
          forvalues year = 2023(-1)2013 {
          di `event' " " `year'
          label variable q23a`event'_`year' "Q23A`event'.Year of `lbl`event'':`year'"
          }
          }
          Amazing!!! Thank you very, very much, Nick!

          Comment

          Working...
          X