Announcement

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

  • positive number stata

    Hi there!

    I want to calculate exchange rate volatility between countries

    Hence I am trying to generate a variable by subtracting exchanges rates from one another but I do not want the value to be below 0

    Hence I only want positive numbers

    Therefore x= |2-3) = 1

    Could someone help me with this please?

    It would help me a lot!


    Thank you so much!


    Andy

  • #2
    Use abs() for absolute value:

    Code:
    gen new_var=abs(var1-var2)
    Stata/MP 14.1 (64-bit x86-64)
    Revision 19 May 2016
    Win 8.1

    Comment


    • #3
      Carole gives good advice as usual. Note that another way to do it, not so good, but worth knowing, is

      Code:
      gen new_var = max(var1, var2) - min(var1, var2)
      EDIT. For some years I tried to hint to others that a quick introductory tour of important functions could be useful to people learning Stata. Eventually I gave up and tried to write it.

      http://www.stata-journal.com/sjpdf.h...iclenum=dm0058

      Naturally, other functions could be very important to you too.
      Last edited by Nick Cox; 05 Apr 2016, 10:25.

      Comment


      • #4
        Andy londale an important, but often overlooked issue raised by Bill Gould (StataCorp) should be considered here as well. Depending on the scale of your variables, you could run into issues with subtraction related to floating point math. You can view a copy of the slides from Bill Gould's presentation here.

        Comment


        • #5
          Thank you Carole J. Wilson and Nick Cox that was very helpful. I have used your suggested code above to create an age difference variable. I'd also like to add in gender as I want to determine whether there is a difference when the female is older than the male (say by 5 or 10 years) and vice versa. Can you kindly amend your code above to achieve this. Kind regards, Chris

          Comment


          • #6
            We need a data example for #5. What are the variables?

            Comment


            • #7
              Thank you for your reply Nick Cox. Yes of course, sample data attached. I'm doing a survival analysis testing the effect of a number of variables on the stability of relationships (de facto and marriages), and as part of this analysis I want to control for difference in age. In particular, to test the effect if female_age is greater than male_age and if the effect changes if this age difference is greater than say 5 or 10 years (and vice versa if male_age > female_age).
              Code:
              * Example generated by -dataex-. To install: ssc install dataex
              clear
              input long(id p_id) byte wave int(hgage p_hgage) byte(hgsex p_hgsex)
              101  102  1 42 48 1 2
              101  102  2 43 49 1 2
              101  102  3 44 50 1 2
              101  102  4 45 51 1 2
              106 142 10 23 30 2 1
              106 142 11 24 31 2 1
              106 142 12 25 32 2 1
              106 142 13 26 33 2 1
              106 142 14 27 34 2 1
              106 142 15 28 35 2 1
              106 142 16 29 36 2 1
              106 142 17 30 37 2 1
              106 142 18 31 38 2 1
              118  119  1 40 44 2 1
              118  119  2 41 45 2 1
              118  119  3 42 46 2 1
              118  119  4 43 47 2 1
              118  119  5 44 48 2 1
              118  119  6 45 49 2 1
              118  119  7 46 50 2 1
              118  119  8 47 51 2 1
              118  119  9 48 52 2 1
              126 135 10 19 25 2 1
              126 135 11 20 26 2 1
              126 135 12 21 27 2 1
              126 135 13 22 28 2 1
              126 135 14 23 29 2 1
              126 135 15 24 30 2 1
              126 135 16 25 31 2 1
              126 135 17 26 32 2 1
              126 135 18 27 33 2 1
              end
              Is that enough data? I am also controlling for education which I've posted on separately, but now I see is related (https://www.statalist.org/forums/for...ated-by-gender)
              Stata v.15.1. Using panel data. Variables are differentiated by respondent and their partner - "p_" represents value for partner.

              Comment


              • #8
                Thanks for the data example but you refer to variables female_sex and male_sex which I can't see (what would they show? the sex of males is male, etc.). I don't see whether 1 means male and 2 means female or vice versa. But you can negate any result here if you want it the other way round. . In this data example I see only different sex couples. So age difference could be

                Code:
                gen wanted = cond(hgsex == 1 , hgage - p_hgage, cond(hgsex == 2, p_hgage - hgage, .))
                with separate tabulation of (1, 2), (2, 1), (1, 1), (2, 2) couples.

                Comment

                Working...
                X