Announcement

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

  • Labeling values

    Hi Everyone,
    I am facing difficulties regarding labeling values while creating a new variable...
    I have created a variable using more than two variables and I want that the newly created variable can be directly labeled from the variables used to construct the new variable. Let me give you a precise example to further clarify the issue.
    Let’s assume that I have five variables named var1, var2 …. Var5. Var1 has only one response i.e., 1, and is labeled as “Unmarried”, similarly, var2 has only one response i.e., 2, and is labeled as “Currently married”, similarly, var3 has only one response i.e., 3, and is labeled as “Divorced”, likewise, var4 has only one response i.e., 4, and is labeled as “Separated”, & finally, var5 also has only one response i.e., 5, and is labeled as “Widowed”.
    I have used the following command for generating marital status variable,
    gen mar_st = max(var1, var2, var3, var4, var5)
    The variable is constructed perfectly with responses 1,2,3,4 and 5. But the problem is I have to use label define and then label all the values to get the variable in a better/perfect shape for my analysis.
    The real problem is in my analysis I have to use almost 8, 10, 12, or more variables to create a single variable. I have been looking for a solution but no luck and then I thought to reach you for help.
    Thanking in anticipation....

  • #2
    Code:
    clear
    input var1 var2 var3 var4 var5
      1  .  .  .  .
      .  2  .  .  .
      .  .  3  .  .
      .  .  .  4  .
      .  .  .  .  5
      .  2  .  4  .
      .  .  .  .  .
    end
    
    label variable var1 "unmarried"
    label variable var2 "currently married"
    label variable var3 "divorced"
    label variable var4 "separated"
    label variable var5 "widowed"
    
    tempvar inconsist
    egen `inconsist' = rownonmiss(var1 var2 var3 var4 var5)
    gen wanted = cond(`inconsist' > 1,.a,max(var1, var2, var3, var4, var5))
    
    local val = 0
    foreach v of varlist var1 var2 var3 var4 var5 {
       local ++val
       local lab : variable label `v'
       label define wanted `val' "`lab'", add
    }
    label define wanted .a "inconsistent", add
    label values wanted wanted
    
    list wanted

    Comment


    • #3
      Dirk Enzmann Bundle of thanks..!

      Comment

      Working...
      X