Announcement

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

  • changing negative values to positive

    Hi statalist,
    How can I change the sign of an observation in data ? I want to change negative values to positive and I tried this code:

    Code:
    replace -X = X if X<0
    the result is :
    (- invalid name)

  • #2
    Lina:
    Code:
    replace x=x*(-1) if x<0
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      thank you Carlo

      Comment


      • #4
        alternatively:

        Code:
        replace x = abs(x)
        ---------------------------------
        Maarten L. Buis
        University of Konstanz
        Department of history and sociology
        box 40
        78457 Konstanz
        Germany
        http://www.maartenbuis.nl
        ---------------------------------

        Comment


        • #5
          Hi Maarten
          Just simply with some mathemathics !
          thank you

          Comment


          • #6
            Lina,
            Maarten solution (#4) is the more appropriate here, and very clean, but let me add something.

            If your code didn't work this is because you wrote it in the wrong way, but your idea was good enough.
            When using replace, to the left of the equal sign, you should indicate the variable you want to change, and to the right its new value.
            So:
            Code:
            replace x=-x if x<0
            Also works (I just moved x to the left and -x to the right compared to your code in #1)
            The way you wrote it, make Stata search for a "-x" variable name, which moreover is an impossible variable name in Stata.

            Despite Maarten and Carlo excellent solutions for your current issue, I think it could be useful for you to properly handle the replace command.

            Best,
            Charlie

            Comment

            Working...
            X