Announcement

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

  • how to create a squared variable in Stata?

    Hello folks,
    I have a age variable and I want to create its square. I would be very grateful if someone could explain me how can I do that. Thank you so much

  • #2
    It should just be:

    Code:
    gen age2=age^2
    where age2 is the name of the new (squared) variable, and age is the original variable.

    Comment


    • #3
      As a warm-up

      Code:
      display 2^2
      For the main exercise

      Code:
       
      gen agesq = age^2
      See

      Code:
      help operators

      Comment


      • #4
        Thank you all so much. Worked for me

        Comment


        • #5
          However, you may not want to. If age squared is going to be an independent variable in your model, you can do things like

          reg y age c.age#c.age

          See -help fvvarlist-.
          -------------------------------------------
          Richard Williams, Notre Dame Dept of Sociology
          StataNow Version: 19.5 MP (2 processor)

          EMAIL: [email protected]
          WWW: https://www3.nd.edu/~rwilliam

          Comment


          • #6
            Shirzaad:
            from a different corner, you might want to exploit the wonderful -fvvarlist- capabilities:
            Code:
            g sq_age=c.age#c.age
            PS: crossed in the cyberspace with Richard's reply.
            Kind regards,
            Carlo
            (Stata 19.0)

            Comment


            • #7
              Shirzaad:
              As per Richard's example, -fvvarlist- can also support you in creating a squared term as a predictor for regression model.
              Kind regards,
              Carlo
              (Stata 19.0)

              Comment

              Working...
              X