Announcement

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

  • Defining Labels with If Statements

    [CODE]
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str4 fileid str2 quarter float hhtype byte sector
    "FVH1" "Q5" 2 1
    "FVH1" "Q5" 2 1
    "FVH1" "Q5" 2 1
    "FVH1" "Q5" 2 2
    "FVH1" "Q5" 2 1
    "FVH1" "Q5" 1 1
    "FVH1" "Q5" 1 2
    "FVH1" "Q5" 1 1
    "FVH1" "Q5" 3 2
    "FVH1" "Q5" 1 1
    "FVH1" "Q5" 3 1
    "FVH1" "Q5" 2 1
    "FVH1" "Q6" 5 1
    "FVH1" "Q6" 1 1

    Given this data, I want to define labels for HHTYPE based on the different values for sector. For example, HHTYPE==1 should be labelled as "agriculture" when sector==1 but "factory" when sector==2. However, I am unable to use label define with if conditions. Any advice is greatly appreciated.

  • #2
    That can't be done. Really, you have a composite variable defined jointly. So, do something like

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str4 fileid str2 quarter float hhtype byte sector
    "FVH1" "Q5" 2 1
    "FVH1" "Q5" 2 1
    "FVH1" "Q5" 2 1
    "FVH1" "Q5" 2 2
    "FVH1" "Q5" 2 1
    "FVH1" "Q5" 1 1
    "FVH1" "Q5" 1 2
    "FVH1" "Q5" 1 1
    "FVH1" "Q5" 3 2
    "FVH1" "Q5" 1 1
    "FVH1" "Q5" 3 1
    "FVH1" "Q5" 2 1
    "FVH1" "Q6" 5 1
    "FVH1" "Q6" 1 1
    end 
    
    egen both = group(hhtype sector)
    sort both 
    list, sepby(both)
    Now you can define your labels. Naturally, some other ordering of categories may appeal more.

    For a little more general discussion see https://www.stata-journal.com/articl...article=dm0034 (free access without subscription).
    Last edited by Nick Cox; 09 Jul 2020, 15:46.

    Comment


    • #3
      Code:
      generate ntype=10*sector+hhtype
      label define ntype 11 "agriculture" 21 "factory"
      label values ntype ntype
      
      list
      Code:
           +--------------------------------------------------+
           | fileid   quarter   hhtype   sector         ntype |
           |--------------------------------------------------|
        1. |   FVH1        Q5        2        1            12 |
        2. |   FVH1        Q5        2        1            12 |
        3. |   FVH1        Q5        2        1            12 |
        4. |   FVH1        Q5        2        2            22 |
        5. |   FVH1        Q5        2        1            12 |
           |--------------------------------------------------|
        6. |   FVH1        Q5        1        1   agriculture |
        7. |   FVH1        Q5        1        2       factory |
        8. |   FVH1        Q5        1        1   agriculture |
        9. |   FVH1        Q5        3        2            23 |
       10. |   FVH1        Q5        1        1   agriculture |
           |--------------------------------------------------|
       11. |   FVH1        Q5        3        1            13 |
       12. |   FVH1        Q5        2        1            12 |
       13. |   FVH1        Q6        5        1            15 |
       14. |   FVH1        Q6        1        1   agriculture |
           +--------------------------------------------------+

      Comment

      Working...
      X