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


                • #9
                  Thank you for your reply Nick Cox. I see now that I did not clearly define the variables in my data example (did I say female_sex, male_sex?). Anyway, to clarify, the gender variables 'hgsex' (respondent's gender) and 'p_hgsex' (their partner's gender) and the age variables 'hgage' (respondent's age) and 'p_hgage' (their partner's age).

                  I want to test the effect of age and gender on the survival rates of couples, e.g. the effect of a male respondent (hgsex == 1 & hgage) older than their female partner (p_hgsex == 2 & p_hgage), etc. I hope this is clearer?

                  Comment


                  • #10
                    My apologies: your text referred to female_age and male_age -- which were not, and are not, evident in #7. So I misquoted you in referring to female_sex and male_sex.That was careless on my part. But otherwise I did understand you correctly and so consider that #8 should answer your question. The code returns, I believe,

                    male age MINUS female age

                    for all different-sex couples.

                    Comment


                    • #11
                      Thank you Nick Cox. After running your concise code I see the result is 'male age - female age', It seems that the condition grabs age (either respondent or partner for each sex) but instead of subtracting age between sexes it looks like your subtracting partner from respondent age within each sex (even though it's obviously not). Could you kindly clarify how this works.

                      Also, as the code subtracts female age from male age the result is sometimes negative, i'm not sure how to interpret this in a Cox regression compared to a categorical variable where = 1 if male age > female age or = 2 if female age > male age (and then being able to control for extent of age difference, say if male age > female age by 5 or 10 years).
                      Code:
                      with separate tabulation of (1, 2), (2, 1), (1, 1), (2, 2) couples
                      Are these tabulations of couples of all possible gender combinations, i.e. 1, 2 - male/female, etc?

                      Thank you in advance for your help and patience.
                      Last edited by Chris Boulis; 06 Aug 2020, 17:53.

                      Comment


                      • #12
                        A request to clarify my code implies that it was unclear, but in turn I don't know what was unclear. The single line of code in #8 may be more transparent if we borrow a name from elsewhere and call cond() instead ifelse() noting that the translation of

                        Code:
                        gen wanted = cond(hgsex == 1 , hgage - p_hgage, cond(hgsex == 2, p_hgage - hgage, .))
                        to
                        Code:
                           
                         gen wanted = ifelse(hgsex == 1 , hgage - p_hgage, ifelse(hgsex == 2, p_hgage - hgage, .))
                        is now a mix of Stata code and translation. If we make that more pseudocode it would become IF hgsex == 1 THEN CALCULATE hgage - p_hgage ELSE IF hgsex == 2 THEN CALCULATE p_hgage - hgage ELSE RETURN missing So if the reporting person is male, your calculation is their age MINUS their partner's age which for different sex couples is male age MINUS female age (and for same sex male couples if you have any it is male age MINUS male partner's age) whereas if the reporting person is female, your calculation is their partner's age MINUS their age which for different sex couples is also male age MINUS female age (and for same sex female couples if you have any it is female age MINUS female partner's age). Sorry, but I have not read all your posts and don't remember all of what I have read, but in principle couples could be (1, 2) reporter male, partner female (1, 1) both male (2, 1) reporter female, partner male (2, 2) both female As said, I don't recall whether your data includes same sex couples. Nor do I know whether it is important who reports and who answers the question. That's the intent any way. More on cond() at https://www.stata-journal.com/articl...article=pr0016 and further examples in https://journals.sagepub.com/doi/pdf...36867X19830921 (if you have access).

                        Comment


                        • #13
                          Let me rewrite the latter part more readably.


                          If we make that more pseudocode it would become

                          IF hgsex == 1
                          THEN CALCULATE hgage - p_hgage
                          ELSE IF hgsex == 2
                          THEN CALCULATE p_hgage - hgage
                          ELSE RETURN missing

                          So

                          if the reporting person is male, your calculation is their age MINUS their partner's age which for different sex couples is male age MINUS female age

                          (and for same sex male couples if you have any it is male age MINUS male partner's age)

                          whereas

                          if the reporting person is female, your calculation is their partner's age MINUS their age which for different sex couples is also male age MINUS female age

                          (and for same sex female couples if you have any it is female age MINUS female partner's age).

                          Sorry, but I have not read all your posts and don't remember all of what I have read, but in principle couples could be

                          (1, 2) reporter male, partner female
                          (1, 1) both male
                          (2, 1) reporter female, partner male
                          (2, 2) both female

                          As said, I don't recall whether your data includes same sex couples. Nor do I know whether it is important who reports and who answers the question.

                          That's the intent any way.

                          More on cond() at https://www.stata-journal.com/articl...article=pr0016 and further examples in https://journals.sagepub.com/doi/pdf...36867X19830921 (if you have access).

                          Comment


                          • #14
                            Thank you Nick Cox for clarifying. And thank you for the links to the two articles - very helpful.

                            To check I do understand, in a situation where I have all male/female couples (differing only by respondent/partner), then this code will always calculate male age - female age - whether male respondent - female partner or male partner - female respondent. Given 'wanted' will contain both positive and negative values could such a variable be included in a Cox regression and if so how would I interpret the result, compared to a (multistate) categorical variable where = 0 if male age == female age [0], = 1 if male age > female age (+) or = 2 if female age > male age [-]. That said, I could test the effect of an age difference of say 10 years, by adding "if wanted == 10" to a statement. Kind regards, Chris.

                            Comment


                            • #15
                              Despite the name I don't know much about Cox regressions and but I've never heard that predictors that may be positive or negative are problematic.

                              I am always in favour of measured differences being used whenever available rather than crude and arbitrary reductions to categorical scales, even ordered ones.
                              I am currently working my way through

                              Gelman, A., Hill, J., Vehtari, A. 2021. Regression and Other Stories. Cambridge: Cambridge University Press.

                              which makes that standard point very well, and many others. (Contrary to https://www.cambridge.org/core/books...076E54E40C372C at this moment, this book has been published, and the date 2021 is just publisher's inflation.)

                              Note that your hypothetical categorical scale is not even ordered correctly as no difference between male and female ages surely goes between positive and negative differences.

                              There is always the possibility that an age difference of say 50 years either way produces an outlier, but see what happens.

                              It seems you are still struggling with understanding #8 but I don't have another way to explain it. If struggling the best tactic is to look carefully at an example where you can work out with mental arithmetic what you want and check that the code is doing it.

                              Taking your helpful data example from #7 and removing irrelevant or repetitive detail, I get this, which you can check to see if it is what you want.


                              Code:
                              * Example generated by -dataex-. To install: ssc install dataex
                              clear
                              input long(id p_id) int(hgage p_hgage) byte(hgsex p_hgsex)
                              101 102 42 48 1 2
                              106 142 23 30 2 1
                              118 119 40 44 2 1
                              126 135 19 25 2 1
                              end
                              
                              gen wanted = cond(hgsex == 1 , hgage - p_hgage, cond(hgsex == 2, p_hgage - hgage, .))
                              
                              list 
                              
                                   +---------------------------------------------------------+
                                   |  id   p_id   hgage   p_hgage   hgsex   p_hgsex   wanted |
                                   |---------------------------------------------------------|
                                1. | 101    102      42        48       1         2       -6 |
                                2. | 106    142      23        30       2         1        7 |
                                3. | 118    119      40        44       2         1        4 |
                                4. | 126    135      19        25       2         1        6 |
                                   +---------------------------------------------------------+

                              Comment

                              Working...
                              X