Announcement

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

  • Problem with ratio in if command

    Hi!

    I have an issue with my dummies that I try to create. I am looking into unequal bequests and want a dummy for when the bequest is not equal.
    I have 4 different groups for different numbers of children among the deceased, and for 2, 4 and 5 children I have no problem as these are finite values (0.5, 0.25, 0.2) but when try to do the same for the group with 3 children I cannot get it to work. I believe this is because 1/3 is not a finite number. This is the command I have tried:

    * create dummy for if bequest is equally divided
    generate equalbequest = 0
    replace equalbequest = 1 if share_bequest != 0.5 & a_antal_barn == 2 & year == 2004
    replace equalbequest = 1 if share_bequest != .3333333 & a_antal_barn == 3 & year == 2004
    replace equalbequest = 1 if share_bequest != 0.25 & a_antal_barn == 4 & year == 2004
    replace equalbequest = 1 if share_bequest != 0.2 & a_antal_barn == 5 & year == 2004
    replace equalbequest = . if missing(a_antal_barn)
    replace equalbequest = . if year !=2004


    I have also tried to set != 1/3 and != (1/3).
    Does anyone have any suggestions to what I should do?

    Best,

    Anna

  • #2
    Hey Anna,

    could you please post some of your data using the dataex command and post the original Stata output for us in order to be able to understand which command doesn't work exactly? Otherwise its hard to guess which part of your command is troubling you. I can recommend you the xtile/pctile command to divide continuous variables into parts, maybe that helps?

    Regards,

    Benedikt

    Comment


    • #3
      1/3 is finite in base 3, and so on, but I think you have the nub of the matter: it's getting exact decimal representations. Your code seems to boil down to

      Code:
      generate unequalbequest = share_bequest != (1/antal_barn) if !missing(share_bequest, antal_barn)
      where

      1. I changed the variable name. Your indicator (you say dummy) is 1 if the bequest is unequal.

      2. You get Stata to do the arithmetic in all cases.

      3. year == 2004 is something else.

      4. Your code would never yield missings, as you start with 0 and conditionally change to 1.

      Comment


      • #4
        Thank you Nick Cox, your code was helpful, but I still have the issue where my individuals who received a bequest of 1/3 get treated as if they had received an unequal bequest.
        I created my share_bequest by dividing their part of the total bequest (b_arvslott_1) by the total bequest, is it here my issue lies?

        *create total bequest per deceased
        egen totalbequest = total(b_arvslott_1_r) if year == 2004, by(a_id)

        *create share of total bequest
        gen share_bequest = b_arvslott_1_r / totalbequest if year == 2004

        (the bequest is received in 2004)

        Is there a way to solve my issue?

        Comment


        • #5
          Please give a data example explaining your problem. FAQ Advice #12 explains.

          Comment


          • #6
            Code:
            * Example generated by -dataex-. To install: ssc install dataex
            clear
            input float(b_arvslott_1_r totalbequest share_bequest unequalbequest)
              81500  244500   .3333333 1
              81500  244500   .3333333 1
              81500  244500   .3333333 1
             479500 1438500   .3333333 1
             479500 1438500   .3333333 1
             479500 1438500   .3333333 1
             315300  945900   .3333333 1
             315300  945900   .3333333 1
             315300  945900   .3333333 1
              10000  307000  .03257329 1
             102900  307000  .33517915 1
              10000  307000  .03257329 1
              10000  307000  .03257329 1
             102900  307000  .33517915 1
                  0  307000          0 1
              30600  307000  .09967427 1
              10000  307000  .03257329 1
              30600  307000  .09967427 1
              81400  244200   .3333333 1
              81400  244200   .3333333 1
              81400  244200   .3333333 1
              87400  262200   .3333333 1
              87400  262200   .3333333 1
              87400  262200   .3333333 1
              73500 1058700  .06942476 1
              73500 1058700  .06942476 1
             205600 1058700  .19420044 1
             148100 1058700  .13988854 1
             205500 1058700    .194106 1
             205500 1058700    .194106 1
              73500 1058700  .06942476 1
              73500 1058700  .06942476 1
              22900   68700   .3333333 1
              22900   68700   .3333333 1
              22900   68700   .3333333 1
             309800 1379500   .2245741 1
             153300 1379500  .11112722 1
             153300 1379500  .11112722 1
              50000 1379500 .036245015 1
              50000 1379500 .036245015 1
              50000 1379500 .036245015 1
              50000 1379500 .036245015 1
             153300 1379500  .11112722 1
              50000 1379500 .036245015 1
             309800 1379500   .2245741 1
              50000 1379500 .036245015 1
              82300  246900   .3333333 1
              82300  246900   .3333333 1
              82300  246900   .3333333 1
                  0  246900          0 1
             135800  457400   .2968955 1
              50000  457400   .1093135 1
             135800  457400   .2968955 1
             135800  457400   .2968955 1
             141800  425400   .3333333 1
             141800  425400   .3333333 1
             141800  425400   .3333333 1
             103500  310500   .3333333 1
             103500  310500   .3333333 1
             103500  310500   .3333333 1
             414800 1244400   .3333333 1
             414800 1244400   .3333333 1
             414800 1244400   .3333333 1
             209800  419600         .5 1
                  0  419600          0 1
             209800  419600         .5 1
                  0  419600          0 1
                  0  419600          0 1
              98900  296700   .3333333 1
              98900  296700   .3333333 1
              98900  296700   .3333333 1
             464200 1392600   .3333333 1
             464200 1392600   .3333333 1
             464200 1392600   .3333333 1
            1151800 3455400   .3333333 1
            1151800 3455400   .3333333 1
            1151800 3455400   .3333333 1
                  0  497200          0 1
              58500  497200   .1176589 1
              58500  497200   .1176589 1
              58500  497200   .1176589 1
              14600  497200  .02936444 1
                  0  497200          0 1
              58500  497200   .1176589 1
              14600  497200  .02936444 1
              58500  497200   .1176589 1
              58500  497200   .1176589 1
              58500  497200   .1176589 1
              58500  497200   .1176589 1
                  0  497200          0 1
              33500  100500   .3333333 1
              33500  100500   .3333333 1
              33500  100500   .3333333 1
                  0  164000          0 1
              82000  164000         .5 1
              82000  164000         .5 1
              12900   38700   .3333333 1
              12900   38700   .3333333 1
              12900   38700   .3333333 1
             211100  663300   .3182572 1
            end

            Comment


            • #7
              Thanks for the example but I see no variable
              antal_barn to check my code in #3. It is possible that you need to go
              Code:
              generate double Share =
              Code:
              b_arvslott_1_r totalbequest
              to keep maximum precision.

              Comment


              • #8
                Here is the dataex with the variable a_antal_barn as well:


                Code:
                * Example generated by -dataex-. To install: ssc install dataex
                clear
                input byte a_antal_barn float(b_arvslott_1_r totalbequest share_bequest unequalbequest)
                2   70900  141800         .5 0
                2   70900  141800         .5 0
                2  201000  402000         .5 0
                2  201000  402000         .5 0
                2 1223300 2446600         .5 0
                2 1223300 2446600         .5 0
                2  334400  668800         .5 0
                2  334400  668800         .5 0
                3   81500  244500   .3333333 1
                3   81500  244500   .3333333 1
                3   81500  244500   .3333333 1
                2  100000  779800    .128238 1
                2  100000  779800    .128238 1
                2  189900  779800    .243524 1
                2  100000  779800    .128238 1
                2  100000  779800    .128238 1
                2  189900  779800    .243524 1
                2       0       0          . .
                2       0       0          . .
                2   53700  107400         .5 0
                2   53700  107400         .5 0
                3  479500 1438500   .3333333 1
                3  479500 1438500   .3333333 1
                3  479500 1438500   .3333333 1
                2  108700  187400  .58004266 1
                2   78700  187400   .4199573 1
                2   55700  167100   .3333333 1
                2   55700  167100   .3333333 1
                2   55700  167100   .3333333 1
                2   93700  187400         .5 0
                2   93700  187400         .5 0
                3  315300  945900   .3333333 1
                3  315300  945900   .3333333 1
                3  315300  945900   .3333333 1
                3   10000  307000  .03257329 1
                3  102900  307000  .33517915 1
                3   10000  307000  .03257329 1
                3   10000  307000  .03257329 1
                3  102900  307000  .33517915 1
                3       0  307000          0 1
                3   30600  307000  .09967427 1
                3   10000  307000  .03257329 1
                3   30600  307000  .09967427 1
                4   81600  326400        .25 0
                4   81600  326400        .25 0
                4   81600  326400        .25 0
                4   81600  326400        .25 0
                2  240800  481600         .5 0
                2  240800  481600         .5 0
                2  107800  215600         .5 0
                2  107800  215600         .5 0
                2  461200  922400         .5 0
                2  461200  922400         .5 0
                3   81400  244200   .3333333 1
                3   81400  244200   .3333333 1
                3   81400  244200   .3333333 1
                4   58600  175800   .3333333 1
                4   58600  175800   .3333333 1
                4   58600  175800   .3333333 1
                4       0  175800          0 1
                2   29300 1333500 .021972254 1
                2  354700 1333500  .26599175 1
                2  506900 1333500   .3801275 1
                2   29300 1333500 .021972254 1
                2   29300 1333500 .021972254 1
                2   29300 1333500 .021972254 1
                2  354700 1333500  .26599175 1
                2  108500  217000         .5 0
                2  108500  217000         .5 0
                4  432300 1729200        .25 0
                4  432300 1729200        .25 0
                4  432300 1729200        .25 0
                4  432300 1729200        .25 0
                3   87400  262200   .3333333 1
                3   87400  262200   .3333333 1
                3   87400  262200   .3333333 1
                2  184600  369200         .5 0
                2  184600  369200         .5 0
                3   73500 1058700  .06942476 1
                3   73500 1058700  .06942476 1
                3  205600 1058700  .19420044 1
                3  148100 1058700  .13988854 1
                3  205500 1058700    .194106 1
                3  205500 1058700    .194106 1
                3   73500 1058700  .06942476 1
                3   73500 1058700  .06942476 1
                3   22900   68700   .3333333 1
                3   22900   68700   .3333333 1
                3   22900   68700   .3333333 1
                2  658400 1316800         .5 0
                2  658400 1316800         .5 0
                2  412500  825000         .5 0
                2  412500  825000         .5 0
                3  309800 1379500   .2245741 1
                3  153300 1379500  .11112722 1
                3  153300 1379500  .11112722 1
                3   50000 1379500 .036245015 1
                3   50000 1379500 .036245015 1
                3   50000 1379500 .036245015 1
                3   50000 1379500 .036245015 1
                end

                Comment


                • #9
                  OK. As said, try

                  Code:
                  gen double Share  = b_arvs / total 
                  
                  gen Unequal = a_antal_barn != 1/Share
                  I find with your example that the cases with 3 and 1/3 are treated as you expect.

                  Comment


                  • #10
                    Thank you so much for your help! gen double did the trick!

                    Comment

                    Working...
                    X