Announcement

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

  • Append number and string with qoutation marks to local variable?

    Hi all,

    I have been spending hours trying to figure out a real simple problem, but I just can't seem to get it. As you can tell, I'm new to stata.

    Background:
    I have a variable taking values from 1 through 10, each corresponding to a specific 6month period. In order to make this more readable I apply a label to it, which I define as:

    Code:
    label define lblName 1 "2005hy1" 2 "2005hy2" 3 "2006hy1" 4 "2006hy2" 5 "2007hy1" 6 "2007hy2" 7 "2008hy1" 8 "2008hy2" 9 "2009hy1" 10 "2009hy2"
    For code-flexibility I now want define this label in a loop (I will have MANY values later on, using different time-brackets), but I'm just don't understand how I best proceed with this.

    Sought after solution:
    I need a loop that creates a variable numberString containing the string
    Code:
    1 "2005hy1" 2 "2005hy2" 3 "2006hy1" 4 "2006hy2" 5 "2007hy1" 6 "2007hy2" 7 "2008hy1" 8 "2008hy2" 9 "2009hy1" 10 "2009hy2"
    so that I later can run a command like
    Code:
    label define `numberString'
    that would evaluate as
    Code:
    label define lblName 1 "2005hy1" 2 "2005hy2" 3 "2006hy1" 4 "2006hy2" 5 "2007hy1" 6 "2007hy2" 7 "2008hy1" 8 "2008hy2" 9 "2009hy1" 10 "2009hy2"
    First step?:
    I've tried almost uncountable variations, but found no solution. I assume the basic code would look something like

    Code:
    local cnt 0
    forvalues y = 2005/2009 {
        forvalues hy = 1/2 {
            // Append the string "`cnt' "`y'hy`hy'" to a local variable called numberString. But I don't know how to do this. How to get the quoutation marks in there, etc?
            local cnt = `cnt' + 1
        }
    }
    label define `numberString'
    But I just can't seem to get it to work... Any ideas or help would be greatly appreciated!!

    Kindly,
    dalaij

  • #2
    Consider the following

    Code:
     
    local y = 2004 
    
    forval j = 1/10 { 
        // if j is odd, it's a new year 
        if mod(`j', 2)  local y = `y' + 1 
        if mod(`j', 2) local suffix = 1 
        else local suffix = 2 
        local label `j'  "`y'hy`suffix'" 
        di  `"`label'"'  
        local labels `labels' `label' 
     } 
     
     di `"`labels'"'
    The bad news is that it takes some time practising small details until you can do this kind of thing faster by more general code than by just doing it by hand. In fact if you have just a few labels to define the slow way is usually faster!

    "Local variable" should always be "local macro" or "local". The word variable doesn't fit it, regardless of what it means outside Stata.

    Comment


    • #3
      You do not have to define value labels in a single command. You can simply add more value associations using the add option as in

      Code:
      local n 1
      forvalues y=2005/2009 {
      label def mylabel `n++' `y'hy1 `n++' `y'hy2, add
      }
      label list

      Comment


      • #4
        Bringing it together:

        Robert's answer is a very nice piece of coding exploiting the fact that it is easy in your case to define labels two at a time. There may often be similar tricks: the hint is to look for the structure in your definitions.

        It's more typical in my experience that a definition of value labels is just a loop over the possible values and their labels. Whether the label define is inside or outside the loop is at choice.

        Comment

        Working...
        X