+ Reply to Thread
Results 1 to 3 of 3

Thread: Trying to create new var, can't figure out if/and options

  1. #1
    Points: 1,223, Level: 19
    Level completed: 23%, Points required for next Level: 77

    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Trying to create new var, can't figure out if/and options




    Hi there,

    I'm a beginner with Stata, so was hoping for some help.

    I have two variables: age and qualification

    The qualification variable looks like this:

    Value 1 CSE or equivalent/GCSE (grades D-G)
    Value 2 O level or equivalent/GCSE (Grades A-C)
    Value 3 A level or equivalent
    Value 4 Degree or equivalent
    Value 5 Postgraduate degree or equivalent
    Value 6 None of these

    I want to construct a years of schooling variable as follows:

    If the individual is over 40 AND has value 6, then in the new variable, years of schooling = 10
    If the individual is under and including 40 AND has value 6, then in the new variable, years of schooling = 11

    For the others, age is not important, so if the value of the qualification variable is 3, for example, the years of schooling value should be 13. If it's 4, years of schooling should be 16 etc

    How would I go about writing a command to do this?

    Thanks

  2. #2
    RoboStataRaptor
    Points: 10,218, Level: 67
    Level completed: 43%, Points required for next Level: 232
    bukharin's Avatar
    Location
    Sydney, Australia
    Posts
    1,291
    Thanks
    11
    Thanked 319 Times in 310 Posts

    Re: Trying to create new var, can't figure out if/and options

    gen years=10 if age>40 & !missing(age) & qualification==6
    replace years=11 if age<=40 & qualification==6
    replace years=13 if qualification==3
    ... etc

    Some things to note:
    = means assign a value
    == means "is equal to"
    use & for logical AND, | for logical OR
    ! means NOT

    You need the:
    & !missing(age)
    because otherwise a missing age will match the expression ">40" (since in Stata, missing values are very very very large numbers)

  3. #3
    Points: 1,223, Level: 19
    Level completed: 23%, Points required for next Level: 77

    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Trying to create new var, can't figure out if/and options


    Fantastic, thank you very much. Appreciate the explanation too.

+ Reply to Thread

           




Similar Threads

  1. Regress function options
    By Yida in forum Stata
    Replies: 3
    Last Post: 04-11-2011, 05:00 AM
  2. Show all selection options in Output
    By Alexneedshelp in forum SPSS
    Replies: 0
    Last Post: 03-03-2011, 05:09 AM
  3. My chances & options? Uncertain math background.
    By Stochast in forum Education and Career
    Replies: 5
    Last Post: 02-05-2011, 04:50 AM
  4. Replies: 1
    Last Post: 09-19-2008, 02:32 PM
  5. Replies: 5
    Last Post: 05-27-2008, 02:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts






Advertise on Talk Stats