Announcement

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

  • Assigning variable labels with use of the variable name

    Dear Colleagues,

    I have a nice data set resembling the extract below.
    Code:
    clear
    input FRAccDwellFireRate20092010 FRAccDwellFireRate20102011 FRAccDwellFireRate20112012 FRAccDwellFireRate20122013
    10 20 30 40
    11 12 13 14 15
    1 2 3 4
    end
    Each of the variables corresponds to Deliberate fires (excluding chimney fires) per 100,000 population (for those interested in the subject that data is publicly available via the Scottish Neighbourhood Statistics). I'm using the code below to nicely label those variables:
    Code:
    foreach var of varlist FRAccDwellFireRate* {
        label variable `var' ///
            `"Deliberate fires (excluding chimney fires) per 100,000 population: `=substr("`var'", 19, 8)' "'
        }
    The code would produce the the following labels.
    Code:
    . codebook, compact
    
    Variable      Obs Unique      Mean  Min  Max  Label
    --------------------------------------------------------------------------------------------------------------------------------------------
    FRA~20092010    3      3  7.333333    1   11  Deliberate fires (excluding chimney fires) per 100,000 population: 20092010
    FRA~20102011    3      3  11.33333    2   20  Deliberate fires (excluding chimney fires) per 100,000 population: 20102011
    FRA~20112012    3      3  15.33333    3   30  Deliberate fires (excluding chimney fires) per 100,000 population: 20112012
    FRA~20122013    3      3  19.33333    4   40  Deliberate fires (excluding chimney fires) per 100,000 population: 20122013
    --------------------------------------------------------------------------------------------------------------------------------------------
    I'm interested in doing something more fancy than that. For instance, I would like to have time periods expressed as 2009/10 not as 20092010 (alternatively 2009-2010). Ideally, would also like to automatically get the required figures in the substr command. Presently, I always look at the variable name and simply count the number figures but it would be wiser, and more efficient, to do this automatically. Ideally I'm looking to for a code where I would only have to provide the first part of the label (in this case: Deliberate fires (excluding chimney fires) per 100,000 population) and the years would be added to the variable label automatically. If it would be possible to implement a switch deciding how the year should be expressed (20092010 or 2009/10), this would be an added bonus.
    Kind regards,
    Konrad
    Version: Stata/IC 13.1

  • #2
    Many ways to do this but if the date always appears at the end and has 8 characters, then this works:
    Code:
    clear
    input FRAccDwellFireRate20092010 FRAccDwellFireRate20102011 FRAccDwellFireRate20112012 FRAccDwellFireRate20122013
    10 20 30 40
    11 12 13 14 15
    1 2 3 4
    end
    
    foreach var of varlist FRAccDwellFireRate* {
        local year `=substr("`var'", -8, 4)'
        local month `=substr("`var'", -4, 2)'
        local day `=substr("`var'", -2, 2)'
        
        label variable `var' `"blah: `year'/`month'/`day'"'
        }
    
    describe
    If the names have a different structure (or no structure), you should describe that.
    You should:

    1. Read the FAQ carefully.

    2. "Say exactly what you typed and exactly what Stata typed (or did) in response. N.B. exactly!"

    3. Describe your dataset. Use list to list data when you are doing so. Use input to type in your own dataset fragment that others can experiment with.

    4. Use the advanced editing options to appropriately format quotes, data, code and Stata output. The advanced options can be toggled on/off using the A button in the top right corner of the text editor.

    Comment


    • #3
      What about

      Code:
      foreach var of varlist FRAccDwellFireRate* {
          label variable `var' ///
              `"Deliberate fires (excluding chimney fires) per 100,000 population: `=substr("`var'", 19, 4)' /`=substr("`var'", 25, 2)'"'
          }
      That deals with the formatting of the dates. The question of automatically finding the position of the date in the variable name is harder to answer because only you know what the other variables you would like to apply this to look like. If the dates always appear as the last 8 characters of the variable name, then it is simple:

      Code:
      local label_stub first_part_of_label_here
      foreach v of varlist whatever {
          label variable `v' `"`label_stub', `=substr("`v'", -8, 4)'/`=substr("`v'", -2, 2)
      }
      But if the dates can appear elsewhere, or if they are not always formatted as XXXXYYYY then something different would be needed.

      Comment


      • #4
        Hi Clyde/Roberto,

        Thank you for showing the interest in my post and the useful comments. I like the proposed approach, it will work for most of the indicators that have the date expressed as 20092010. Presumably, the worthwhile modification could involve counting the end of digits at the end. Ideally, in the code:
        Code:
        `=substr("`v'", -8, 4)'
        the -8 would correspond to the number of digits on the end of the variable name. Of course the code
        Code:
        =substr("`v'", -2, 2)
        would have to be then altered accordingly.
        Kind regards,
        Konrad
        Version: Stata/IC 13.1

        Comment


        • #5
          [QUOTE=Clyde Schechter;n633791]What about
          Code:
          local label_stub first_part_of_label_here
          foreach v of varlist whatever {
          label variable `v' `"`label_stub', `=substr("`v'", -8, 4)'/`=substr("`v'", -2, 2)
          }
          Just to add to this, I think that the code should be:
          Code:
          * Rename selected fire variables.
          local label_stub "Deliberate fires (excluding chimney fires) per 100,000:"
          foreach v of varlist frdel_fire_rate* {
              label variable `v' `"`label_stub', `=substr("`v'", -8, 4)' / `=substr("`v'", -2, 2) "'
          }
          Kind regards,
          Konrad
          Version: Stata/IC 13.1

          Comment


          • #6
            Yes, you are quite right about the final compound double quote.

            Comment

            Working...
            X