Announcement

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

  • Minimum between two variables

    Hello,

    I am looking to generate new variable that is a minimum of two variables.

    For example

    age age2 New_Age
    5 6 5
    2 1 1
    4 3 3
    7 5 5


    how would I create New_Age?

    Thank you

  • #2
    Code:
    gen New_Age = min(age, age2)
    In the future, when showing data examples, please use the -dataex- command to do so. If you are running version 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    Comment


    • #3
      Thank you but that command doesn't work. It provide this error "unknown function Min() "

      Comment


      • #4
        Not Min, min. Everything in Stata is case-sensitive.

        Comment


        • #5
          Originally posted by Clyde Schechter View Post
          Not Min, min. Everything in Stata is case-sensitive.
          It seems this case is calling for a -rowmin()-

          Sal, try this:
          Code:
          gen New_Age = rowmin(age age2)
          Last edited by Ken Chui; 19 Jul 2021, 13:05.

          Comment


          • #6
            Ken Chui - no, rowmin is a function of -egen- not a general function that can be used with -gen-; so Sal Al-Aq can either use what Clyde Schechter suggested in #2 or can modify #4 to use -egen- rather than -gen-

            Comment


            • #7
              Brief discussion of -egen, rowmin()- vs -gen ... = min(...)-.

              The -gen- function -min()- is more flexible in that it can accommodate any expressions as its arguments. By contrast the -egen, rowmin()- function is applicable only to variables. So if you wanted the minimum of x+y or z*w, you would have to use -gen-'s -min()- function. If you are deep inside many nested loops, -gen ... = min(...)- is also faster than -egen, rowmin()-, though there are really few situations where the difference would be noticeable. So why would one ever use -egen, rowmin()-? Well, whereas -gen ... = min(...)- requires a comma separated list of arguments, -egen, rowmin()- accepts a standard Stata varlist, including wildcards. So if you are looking for the minimum value of a large number of variables that can be briefly described with a wildcard expression, -egen, rowmin()- will be easier both to write and read. If you have expressions, not variables, -gen ... = min(...)- is mandatory. If, as in #1, you need the minimum of a small number of variables that can be spelled out quickly, either one is equally good.

              Comment


              • #8
                Originally posted by Rich Goldstein View Post
                Ken Chui - no, rowmin is a function of -egen- not a general function that can be used with -gen-; so Sal Al-Aq can either use what Clyde Schechter suggested in #2 or can modify #4 to use -egen- rather than -gen-
                Thanks Rich, I did mean to say egen. Thanks for the correction.

                Comment


                • #9
                  Originally posted by Clyde Schechter View Post
                  Brief discussion of -egen, rowmin()- vs -gen ... = min(...)-.

                  The -gen- function -min()- is more flexible in that it can accommodate any expressions as its arguments. By contrast the -egen, rowmin()- function is applicable only to variables. So if you wanted the minimum of x+y or z*w, you would have to use -gen-'s -min()- function. If you are deep inside many nested loops, -gen ... = min(...)- is also faster than -egen, rowmin()-, though there are really few situations where the difference would be noticeable. So why would one ever use -egen, rowmin()-? Well, whereas -gen ... = min(...)- requires a comma separated list of arguments, -egen, rowmin()- accepts a standard Stata varlist, including wildcards. So if you are looking for the minimum value of a large number of variables that can be briefly described with a wildcard expression, -egen, rowmin()- will be easier both to write and read. If you have expressions, not variables, -gen ... = min(...)- is mandatory. If, as in #1, you need the minimum of a small number of variables that can be spelled out quickly, either one is equally good.
                  Hey Clyde, thanks for the detailed explanation.

                  Comment

                  Working...
                  X