Announcement

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

  • a better method of generating a variable?

    I have a education variable, q15_educ, in my data set and I need to create a new education variable, educ, based on that. I have a method, but do you have easier method?

    Click image for larger version

Name:	WechatIMG1036.png
Views:	1
Size:	45.8 KB
ID:	1540226


    Code:
    gen educ = 1 if (q15_educ == "High school graduate, diploma or equivalent") | (q15_educ == "Some high school") | (q15_educ == "Trade/Technical/vocational training")
    replace educ = 2 if (q15_educ == "Associate degree") | (q15_educ == "Bachelor's degree") | (q15_educ == "Some college credit, no degree") 
    replace educ = 3 if (q15_educ == "Doctorate degree") | (q15_educ == "Master's degree")

  • #2
    Hi Yao,

    Here is what you should try:

    Code:
    tab q15_educ, nolab
    gen educ =1 if q15_educ==4 | q15_educ==8 | q15_educ==9
    replace educ=2 if educ==. & (q15_educ==1 | q15_educ==2 | q15_educ==6)
    replace educ=3 if educ==. & (q15_educ==3 | q15_educ==5)
    Last edited by Antoine Dedewanou; 07 Mar 2020, 20:38.

    Comment


    • #3
      Hi Antoine,

      Your method doesn't work. Stata prompts "type mismatch", because q15_educ is a string variable.

      Comment


      • #4
        Hi Yao,

        Obviously, my method works if q15_educ is a discrete variable with the values ranging from 1 to 9. Regarding the code that you wrote initially, can you try this?

        Code:
        gen educ = 1 if (q15_educ == "High school graduate, diploma or equivalent") | (q15_educ == "Some high school") | (q15_educ == "Trade/Technical/vocational training")
        replace educ = 2 if educ==. & ((q15_educ == "Associate degree") | (q15_educ == "Bachelor's degree") | (q15_educ == "Some college credit, no degree"))
        replace educ = 3 if educ==. & ((q15_educ == "Doctorate degree") | (q15_educ == "Master's degree"))
        Last edited by Antoine Dedewanou; 07 Mar 2020, 21:36.

        Comment


        • #5
          Well, you just repeat my codes...

          Comment


          • #6
            Code:
            help inlist()
            to learn why (e.g.) this is simpler

            Code:
             
             replace educ = 3 if educ==. & inlist(q15_educ, "Doctorate degree", "Master's degree")

            Comment

            Working...
            X