Announcement

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

  • Looping over value label and variable label

    Hi,
    I am defining and attaching value and variable labels, and I really need help on how to use loops to accomplish them. Any practical insights in this direction?
    a) How can I apply a loop over the definition and attachment of the following value labels?

    label define ltrtd 0 "NSW control" 1 "NSW treated"
    label define lblk 0 "else" 1 "black"
    label define lmar 0 "else" 1 "married"
    label define lnod 0 "H.S. diploma" 1 "no diploma"
    label define lhis 0 "else" 1 "hispanic"
    label define lsam 1 "NSW randomized" 2 "CPS comparison" "PSID comparison"

    label values treated ltrtd
    label values black lblk
    label values married lmar
    label values nodegree lnod
    label values hisp lhis
    label values sample lsam

    b) How can I apply a loop to label the following variables?

    label variable treated "NSW randomized group"
    label variable age "age of individual"
    label variable educ "years of schooling"
    label variable black "race is black"
    label variable married "status is married"
    label variable nodegree "no high school diploma"
    label variable re78 "real $ earnings in 1978"
    label variable hisp "race is hispanic"
    label variable sample "NSW, CPS and PSID sample"

    Thanks in advance for your help.

    Eric


  • #2
    I am curious for another answer, but I think there is little to gain from a loop here. Have a look into labutil2 (SSC) for ideas.

    Best
    Daniel

    Comment


    • #3
      label define ltrtd 0 "NSW control" 1 "NSW treated"
      label define lblk 0 "else" 1 "black"
      label define lmar 0 "else" 1 "married"
      label define lnod 0 "H.S. diploma" 1 "no diploma"
      label define lhis 0 "else" 1 "hispanic"
      label define lsam 1 "NSW randomized" 2 "CPS comparison" "PSID comparison"
      I don't see any potential for making a loop here--there is no repetitive pattern to these commands.

      But the next block
      label values treated ltrtd
      label values black lblk
      label values married lmar
      label values nodegree lnod
      label values hisp lhis
      label values sample lsam
      is almost "loopable" as there is something close to a pattern. Except for the first two variables, the value label's name is letter l followed by the first three characters of the variable name. So these can be captured in a single loop:

      Code:
      foreaach v of varlist married nodegree hisp sample {
          local first_three = substr("`v'", 1, 3)
          label values `v' l`first_three'
      }
      Of course, treated and black don't follow the pattern, so either those label names can be changed to fit the pattern, or separate -label values- commands have to be written for them.

      My usual practice when defining value labels for variables is to give them the same name as the variable itself.* That way it is very easy to apply them all in a loop like
      Code:
      foreach v of varlist whatever {
          label values `v' `v'
      }
      So Mr. Owusu might want to reconsider his practice for choosing label names.

      Finally, the block of -label variable- commands has no repetitive pattern, so there is no reasonable way to make a loop out of them.

      *Not all the time, of course. Sometimes there is a single value label, for example one assigning 0 to No and 1 to Yes, that will be applied to many variables. In that case I give it a descriptive name such as boolean or yesno. In addition to making it easier to run loops labeling variables, making the value label name the same as the variable name also makes it easier to remember what the value label attached to a variable is when you need to use that information writing code.
      Last edited by Clyde Schechter; 04 Feb 2017, 12:36.

      Comment


      • #4
        Thanks so much for these insights. Real eye opener.

        Comment

        Working...
        X