Announcement

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

  • how to use egen command to create dummy variable

    Dear All,

    I have the following categories of a dummy variable EDUYRDG.

    Click image for larger version

Name:	Eduy.png
Views:	1
Size:	17.8 KB
ID:	1679861


    I want to create three dummy variables for Education. The categories are given below.
    1. LOW EDUCATION (FROM EIGHTGRADE TO nohighschool)
    2. MEDIUM EDUCATION (FROM GED TO VOCATIONAL)
    3. HIGH EDUCATION (FROM ASSOCIATED DEGREE TO MASTERS) and CREATE A DUMMY VARIABLE WITH EACH LEVEL OF EDUCATION.

    How to use egen command to create the above three variable from the original variable.

    Looking forward to your reply

    With regards,
    Upananda Pani

  • #2
    I wouldn't call this variable EDUYRDG a dummy variable, a term best used for a variable that may have just two distinct non-missing values, usually 1 and 0. It is a categorical variable.

    egen cannot help you here.

    We're seeing the value labels. At a guess you want either of these solutions. I create a sandbox dataset to show results.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float EDU
    -9
    -8
    -7
    -1
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    end
    
    recode EDU (1/2=1 "Low") (3/6=2 "Medium") (7/9=3 "High") (else=.), gen(wanted1)
    
    gen wanted2 = 1 if inlist(EDU, 1,2)
    replace wanted2 = 2 if inlist(EDU, 3,4,5,6)
    replace wanted2 = 3 if inlist(EDU, 7, 8, 9)
    label def wanted2 1 "Low" 2 "Medium" 3 "High"
    label val wanted2 wanted2
    
    list 
    
    list, nolabel
    Results:

    Code:
     
    
     . list 
    
         +-------------------------+
         | EDU   wanted1   wanted2 |
         |-------------------------|
      1. |  -9         .         . |
      2. |  -8         .         . |
      3. |  -7         .         . |
      4. |  -1         .         . |
      5. |   1       Low       Low |
         |-------------------------|
      6. |   2       Low       Low |
      7. |   3    Medium    Medium |
      8. |   4    Medium    Medium |
      9. |   5    Medium    Medium |
     10. |   6    Medium    Medium |
         |-------------------------|
     11. |   7      High      High |
     12. |   8      High      High |
     13. |   9      High      High |
     14. |  10         .         . |
         +-------------------------+
    
    . 
    . list, nolabel 
    
         +-------------------------+
         | EDU   wanted1   wanted2 |
         |-------------------------|
      1. |  -9         .         . |
      2. |  -8         .         . |
      3. |  -7         .         . |
      4. |  -1         .         . |
      5. |   1         1         1 |
         |-------------------------|
      6. |   2         1         1 |
      7. |   3         2         2 |
      8. |   4         2         2 |
      9. |   5         2         2 |
     10. |   6         2         2 |
         |-------------------------|
     11. |   7         3         3 |
     12. |   8         3         3 |
     13. |   9         3         3 |
     14. |  10         .         . |
         +-------------------------+
    There are other ways to do it. You may want to map missing values to extended missing values, as in this alternative.


    Code:
    recode EDU (1/2=1 "Low") (3/6=2 "Medium") (7/9=3 "High") (-9=.a "Not ascertained") (-8=.b "Don't know") (-7=.c "Refused") (-1=.d "Inapplicable")(10=.e "Child under 5"), gen(wanted3)

    Comment


    • #3
      Dear Sir,

      Thank you for your reply. I want to create three dummy variable from the EDUYRDG variable.

      With best regards,
      Upananda Pani

      Comment


      • #4
        Just use factor variable notation with one of the variables created by #2. See e.g. Section 4 in https://www.stata-journal.com/articl...article=dm0099
        Last edited by Nick Cox; 30 Aug 2022, 05:49.

        Comment

        Working...
        X