Announcement

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

  • Cant make a new variable HELP picture added





    this is an assignment I got in stata. Anyone can give me the right commands? It keeps saying too many variables specified when I try. I know its probably very simple but im new to stata so please help me. thank you
    Click image for larger version

Name:	coding assignment.png
Views:	1
Size:	48.5 KB
ID:	1760167

  • #2
    Hello Joske,
    since you are obviously not only new to Stata, but also new here to the StataForum, take a few minutes and take a look at the FAQs.

    Let's assume that the variable educ is numeric and has values ​​1 to 20. As you didnt post any example data lets just make up some play data:
    Code:
    clear
    set obs 20
    gen educ = _n
    Then you have many different ways to generate the new variable education. Two very basic ones are:
    1) via generate / replace commands
    Code:
    generate education = .
    replace education = 1 if educ <5
    replace education = 2 if educ >=5 & educ < 9
    replace education = 3 if educ >=9 & educ <13
    replace education = 4 if educ >=13  & educ <17
    replace education = 5 if educ >= 17
    label define educ 1 "Elementary school" 2 "Middle school" 3 "High school" 4 "College" 5"Post Graduate Education"
    label values education educ
    2) via recode
    Code:
    recode educ (0/4 = 1 "Elementary school") (5/8 = 2 "Middle school") (9/12 = 3 "High school") (13/16 = 4 "College") (17 / 20 = 5 "Post Graduate Education") , gen(education2)
    All the best,
    Benno

    Comment


    • #3
      thanks for helping, I tried both methods and it generates the new variables, but now for example if educ is 9 it says post graduate education instead of high school. So the numbers dont match

      Comment


      • #4
        Since you don't provide any information about what exactly you "tried" with which data, I can't be of any more help. The code above does exactly what it is supposed to do. If you have different results, you must have done something different to the provided code. Most likely something wrong.

        Comment


        • #5
          Did you encode your numeric variable from an original string variable?

          Comment


          • #6
            Yes educ was a string variable so i encoded it into a numeric one. Then I generated education and replace them all with the given condition i.e. educ<5 etc. and then labeled them as suggested above. But now if educ has for example a value of 12, it says that the education is Middle School and not High school as it should say.

            Comment


            • #7
              .

              Comment


              • #8
                I do hope Joske will duly credit the help he received here when submitting his assignment.

                The general principle with homework questions is outlined in #4 here.

                Comment

                Working...
                X