Announcement

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

  • How to create a variable which varies depending on another variable

    Hi,

    I want to generate a variable which would be equal to y if variable x were bigger than n or equal to z if x were smaller than or equal to n.

    For example, if age>50, I would like "experience"=age+10, but if age<=50 then I would like "experience"=age*5.

    Is something like this possible?

    Thanks.

  • #2
    Code:
    help cond()
    Example:
    Code:
    generate double experience = cond(age <= 50, 5 * age, age + 10)

    Comment


    • #3
      Thanks a lot!

      Comment


      • #4
        Originally posted by Joseph Coveney View Post
        Code:
        help cond()
        Example:
        Code:
        generate double experience = cond(age <= 50, 5 * age, age + 10)
        Is it possible to do this:

        generate a new variable with the following conditions:

        If eyear>1991 and syear<1991, newvariable= totexp - (eyear - 1991)

        If eyear>1991 and syear>1991, newvariable= totexp + (syear - 1991)

        If eyear <1991, newvariable = difference

        Where eyear, syear, totexp and difference are all existing variables?

        Comment


        • #5
          Originally posted by Ella Ki View Post

          Is it possible to do this:

          generate a new variable with the following conditions:

          If eyear>1991 and syear<1991, newvariable= totexp - (eyear - 1991)

          If eyear>1991 and syear>1991, newvariable= totexp + (syear - 1991)

          If eyear <1991, newvariable = difference

          Where eyear, syear, totexp and difference are all existing variables?
          Yes. You can nest the function.
          Code:
          generate double new_variable = cond(eyear < 1991, difference, cond(syear < 1991, totexp - eyear - 1991, totexp + syear - 1991))
          replace new_variable = . if eyear == 1991 | syear == 1991

          Comment


          • #6
            It is certainly possible to nest calls to -cond()- as shown by Joseph. But the resulting code is difficult to look at and understand. I think -cond()- works fine for a simple two-way calculation: do this in one circumstance and do that otherwise. But where you have 3 or more branches, I think it is better to write things out more clearly as:

            Code:
            gen newvariable = totexp - (eyear-1991) if eyear > 1991 & syear < 1991
            replace newvariable = totexp + (syear - 1991) if eyear > 1991 & syear > 1991
            replace newvariable = difference if eyear < 1991
            If you come back to this code in a few months (or even a few days) you can look at it and understand at a glance what it does. If you use nested -cond()- you will probably struggle to grasp it.

            By the way, completely as an aside, your criteria do not cover the possibility that eyear == 1991. Is it supposed to be that way, or is that an error?

            Comment


            • #7
              Originally posted by Clyde Schechter View Post
              It is certainly possible to nest calls to -cond()- as shown by Joseph. But the resulting code is difficult to look at and understand. I think -cond()- works fine for a simple two-way calculation: do this in one circumstance and do that otherwise. But where you have 3 or more branches, I think it is better to write things out more clearly as:

              Code:
              gen newvariable = totexp - (eyear-1991) if eyear > 1991 & syear < 1991
              replace newvariable = totexp + (syear - 1991) if eyear > 1991 & syear > 1991
              replace newvariable = difference if eyear < 1991
              If you come back to this code in a few months (or even a few days) you can look at it and understand at a glance what it does. If you use nested -cond()- you will probably struggle to grasp it.

              By the way, completely as an aside, your criteria do not cover the possibility that eyear == 1991. Is it supposed to be that way, or is that an error?
              Yes you are right, I think I meant to write <= in the last one.

              Thanks everyone!

              Comment

              Working...
              X