Announcement

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

  • Counting observations within specific group

    Dear Statlists,


    My data look like this,

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str5 Person_ID str29 Firm_ID int year double(Blue Red) float(count_cycles Total_red_each_cycle Total_blue_each_cycle Total_cycle)
    "00013" "00105510" 1995      0  2000  1  3000   1000   4000
    "00013" "00105510" 1996   1000  1000  1  3000   1000   4000
    "00013" "00105510" 1997   1000     0  2     0   2000   2000
    "00013" "00105510" 1998   1000     0  2     0   2000   2000
    "00013" "00105510" 1999   3000  1000  3  1000   5000   6000
    "00013" "00105510" 2000   2000     0  3  1000   5000   6000
    "00013" "00105510" 2001      0     0  4     0      0      0
    "00013" "00105510" 2002      0     0  4     0      0      0
    "00013" "00105510" 2003   1000  4000  5  8000   1000   9000
    "00013" "00105510" 2004      0  4000  5  8000   1000   9000
    "00013" "00105510" 2005   2100  1000  6  2000   4200   6200
    "00013" "00105510" 2006   2100  1000  6  2000   4200   6200
    "00013" "00105510" 2007   2300  1000  7 29500   4600  34100
    "00013" "00105510" 2008   2300 28500  7 29500   4600  34100
    "00013" "00105510" 2009      0 30400  8 61900   4800  66700
    "00013" "00105510" 2010   4800 31500  8 61900   4800  66700
    "00013" "00105510" 2011   5000 35800  9 60800   7500  68300
    "00013" "00105510" 2012   2500 25000  9 60800   7500  68300
    "00013" "00105510" 2013      0 34900 10 78700   5200  83900
    "00013" "00105510" 2014   5200 43800 10 78700   5200  83900
    "00013" "00105510" 2015      0     0 11 45900   2866  48766
    "00013" "00105510" 2016   2866 45900 11 45900   2866  48766
    "00013" "00105510" 2017   2700 42700 12 80680   5400  86080
    "00013" "00105510" 2018   2700 37980 12 80680   5400  86080
    "00013" "00105510" 2019   5500 41500 13 41500   5500  47000
    "00013" "00105510" 2020      0     0 13 41500   5500  47000
    "00013" "00105510" 2021      0     0 14     0      0      0
    "00125" "08467070" 1997   1000     0  2     0   2000   2000
    "00125" "08467070" 1998   1000     0  2     0   2000   2000
    "00125" "08467070" 1999   1000  1000  3  1300   3000   4300
    "00125" "08467070" 2000   2000   300  3  1300   3000   4300
    "00125" "08467070" 2001      0     0  4  2300   3000   5300
    "00125" "08467070" 2002   3000  2300  4  2300   3000   5300
    "00125" "08467070" 2003   4000     0  5  2300   6000   8300
    "00125" "08467070" 2004   2000  2300  5  2300   6000   8300
    "00125" "08467070" 2005   2000  2000  6  2000  14700  16700
    "00125" "08467070" 2006  12700     0  6  2000  14700  16700
    "00125" "08467070" 2007   9200     0  7     0  40000  40000
    "00125" "08467070" 2008  30800     0  7     0  40000  40000
    "00125" "08467070" 2009   7200     0  8     0  14400  14400
    "00125" "08467070" 2010   7200     0  8     0  14400  14400
    "00125" "08467070" 2011  43300     0  9     0 128300 128300
    "00125" "08467070" 2012  85000     0  9     0 128300 128300
    "00125" "08467070" 2013  42800  2600 10  2600  83000  85600
    "00125" "08467070" 2014  40200     0 10  2600  83000  85600
    "00125" "08467070" 2015  91500     0 11     0 169102 169102
    "00125" "08467070" 2016  77602     0 11     0 169102 169102
    "00125" "08467070" 2017  65600     0 12     0 138800 138800
    "00125" "08467070" 2018  73200     0 12     0 138800 138800
    "00125" "08467070" 2019 254100     0 13     0 254100 254100
    "01440" "26875P10" 1999      0  1000  3  1000      0   1000
    "01440" "26875P10" 2000      0     0  3  1000      0   1000
    "01440" "26875P10" 2001      0     0  4     0      0      0
    "01440" "26875P10" 2002      0     0  4     0      0      0
    "01440" "26875P10" 2003      0  1000  5  1000      0   1000
    "01440" "26875P10" 2004      0     0  5  1000      0   1000
    "01440" "26875P10" 2005      0     0  6   500      0    500
    "01440" "26875P10" 2006      0   500  6   500      0    500
    "01440" "26875P10" 2007      0     0  7     0      0      0
    "01440" "26875P10" 2008      0     0  7     0      0      0
    "01440" "26875P10" 2009      0  4800  8 17300      0  17300
    "01440" "26875P10" 2010      0 12500  8 17300      0  17300
    "01440" "26875P10" 2011      0  4250  9 15250      0  15250
    "01440" "26875P10" 2012      0 11000  9 15250      0  15250
    "01440" "26875P10" 2013      0  7600 10  7600      0   7600
    end


    I want to create a variable that count the total number of cycles that he /she ONLY CONTRIBUTE during his own period.


    For instance, the person with ID 00013 for firm ID 00105510 from 1995 to 2021, so


    Firstly, I created a variable that capture the the number of election cycles for the whole sample. This variable called "count_cycles" which is
    - from 1995 to 1996 is considered as a first cycle so the variable "count_cycles" = 1
    - from 1997 to 1998 is considered as a second cycle so the variable "count_cycles" = 2
    and so on until 2022 with it ends with 14 cycles


    but, I need to create another variable called "Count_donated_cycles" which capture only the cycles that he/she donate,

    for instance for the first person, based on "Total_cycle" variable , he donated for all cycles except (2001-2002) cycle & (2021-2022) cycle
    therefore if I count it manually, I will ends with the 12 cycles only for this person, so the "Count_donated_cycles" variable = 12
    Similarly, the person with ID 01440 , he donated only to 6 cycles during his period, so the "Count_donated_cycles" variable = 6

    Actually, I tried to use this command but I got incorrect observations:

    Code:
     bysort Person_ID Firm_ID count_cycles : gen Count_donated_cycles= _n if Total_cycle!=0
    Does any one have any ideas how to create this variable ?










  • #2
    Mohamed:
    have you already given a shot to:
    Code:
    .  bysort Person_ID Firm_ID count_cycles : gen Count_donated_cycles= _N if Total_cycle!=0
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      yes Carlo , I did but It doesn't work as well

      Comment


      • #4
        I can't follow what Red Blue and donated mean here, except that working backwards from the answer, I guess that this is what you want.

        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input str5 Person_ID str29 Firm_ID int year double(Blue Red) float(count_cycles Total_red_each_cycle Total_blue_each_cycle Total_cycle)
        "00013" "00105510" 1995      0  2000  1  3000   1000   4000
        "00013" "00105510" 1996   1000  1000  1  3000   1000   4000
        "00013" "00105510" 1997   1000     0  2     0   2000   2000
        "00013" "00105510" 1998   1000     0  2     0   2000   2000
        "00013" "00105510" 1999   3000  1000  3  1000   5000   6000
        "00013" "00105510" 2000   2000     0  3  1000   5000   6000
        "00013" "00105510" 2001      0     0  4     0      0      0
        "00013" "00105510" 2002      0     0  4     0      0      0
        "00013" "00105510" 2003   1000  4000  5  8000   1000   9000
        "00013" "00105510" 2004      0  4000  5  8000   1000   9000
        "00013" "00105510" 2005   2100  1000  6  2000   4200   6200
        "00013" "00105510" 2006   2100  1000  6  2000   4200   6200
        "00013" "00105510" 2007   2300  1000  7 29500   4600  34100
        "00013" "00105510" 2008   2300 28500  7 29500   4600  34100
        "00013" "00105510" 2009      0 30400  8 61900   4800  66700
        "00013" "00105510" 2010   4800 31500  8 61900   4800  66700
        "00013" "00105510" 2011   5000 35800  9 60800   7500  68300
        "00013" "00105510" 2012   2500 25000  9 60800   7500  68300
        "00013" "00105510" 2013      0 34900 10 78700   5200  83900
        "00013" "00105510" 2014   5200 43800 10 78700   5200  83900
        "00013" "00105510" 2015      0     0 11 45900   2866  48766
        "00013" "00105510" 2016   2866 45900 11 45900   2866  48766
        "00013" "00105510" 2017   2700 42700 12 80680   5400  86080
        "00013" "00105510" 2018   2700 37980 12 80680   5400  86080
        "00013" "00105510" 2019   5500 41500 13 41500   5500  47000
        "00013" "00105510" 2020      0     0 13 41500   5500  47000
        "00013" "00105510" 2021      0     0 14     0      0      0
        "00125" "08467070" 1997   1000     0  2     0   2000   2000
        "00125" "08467070" 1998   1000     0  2     0   2000   2000
        "00125" "08467070" 1999   1000  1000  3  1300   3000   4300
        "00125" "08467070" 2000   2000   300  3  1300   3000   4300
        "00125" "08467070" 2001      0     0  4  2300   3000   5300
        "00125" "08467070" 2002   3000  2300  4  2300   3000   5300
        "00125" "08467070" 2003   4000     0  5  2300   6000   8300
        "00125" "08467070" 2004   2000  2300  5  2300   6000   8300
        "00125" "08467070" 2005   2000  2000  6  2000  14700  16700
        "00125" "08467070" 2006  12700     0  6  2000  14700  16700
        "00125" "08467070" 2007   9200     0  7     0  40000  40000
        "00125" "08467070" 2008  30800     0  7     0  40000  40000
        "00125" "08467070" 2009   7200     0  8     0  14400  14400
        "00125" "08467070" 2010   7200     0  8     0  14400  14400
        "00125" "08467070" 2011  43300     0  9     0 128300 128300
        "00125" "08467070" 2012  85000     0  9     0 128300 128300
        "00125" "08467070" 2013  42800  2600 10  2600  83000  85600
        "00125" "08467070" 2014  40200     0 10  2600  83000  85600
        "00125" "08467070" 2015  91500     0 11     0 169102 169102
        "00125" "08467070" 2016  77602     0 11     0 169102 169102
        "00125" "08467070" 2017  65600     0 12     0 138800 138800
        "00125" "08467070" 2018  73200     0 12     0 138800 138800
        "00125" "08467070" 2019 254100     0 13     0 254100 254100
        "01440" "26875P10" 1999      0  1000  3  1000      0   1000
        "01440" "26875P10" 2000      0     0  3  1000      0   1000
        "01440" "26875P10" 2001      0     0  4     0      0      0
        "01440" "26875P10" 2002      0     0  4     0      0      0
        "01440" "26875P10" 2003      0  1000  5  1000      0   1000
        "01440" "26875P10" 2004      0     0  5  1000      0   1000
        "01440" "26875P10" 2005      0     0  6   500      0    500
        "01440" "26875P10" 2006      0   500  6   500      0    500
        "01440" "26875P10" 2007      0     0  7     0      0      0
        "01440" "26875P10" 2008      0     0  7     0      0      0
        "01440" "26875P10" 2009      0  4800  8 17300      0  17300
        "01440" "26875P10" 2010      0 12500  8 17300      0  17300
        "01440" "26875P10" 2011      0  4250  9 15250      0  15250
        "01440" "26875P10" 2012      0 11000  9 15250      0  15250
        "01440" "26875P10" 2013      0  7600 10  7600      0   7600
        end
        
        bysort Person_ID count_cycles (Total_cycle) : gen donated = cond(_n == _N, Total_cycle > 0, 0)
        by Person_ID : egen wanted = total(donated)
        
        tabdisp Person_ID, c(wanted)
        
        ----------------------
        Person_ID |     wanted
        ----------+-----------
            00013 |         12
            00125 |         12
            01440 |          6
        ----------------------
        
        .

        Comment


        • #5
          Mohamed:
          have you already given a shot to:
          Code:
          . bysort Person_ID (Firm_ID): gen Count_donated_cycles= _N if Total_cycle!=0
          Kind regards,
          Carlo
          (Stata 19.0)

          Comment


          • #6
            Originally posted by Nick Cox View Post
            I can't follow what Red Blue and donated mean here, except that working backwards from the answer, I guess that this is what you want.

            Code:
            * Example generated by -dataex-. For more info, type help dataex
            clear
            input str5 Person_ID str29 Firm_ID int year double(Blue Red) float(count_cycles Total_red_each_cycle Total_blue_each_cycle Total_cycle)
            "00013" "00105510" 1995 0 2000 1 3000 1000 4000
            "00013" "00105510" 1996 1000 1000 1 3000 1000 4000
            "00013" "00105510" 1997 1000 0 2 0 2000 2000
            "00013" "00105510" 1998 1000 0 2 0 2000 2000
            "00013" "00105510" 1999 3000 1000 3 1000 5000 6000
            "00013" "00105510" 2000 2000 0 3 1000 5000 6000
            "00013" "00105510" 2001 0 0 4 0 0 0
            "00013" "00105510" 2002 0 0 4 0 0 0
            "00013" "00105510" 2003 1000 4000 5 8000 1000 9000
            "00013" "00105510" 2004 0 4000 5 8000 1000 9000
            "00013" "00105510" 2005 2100 1000 6 2000 4200 6200
            "00013" "00105510" 2006 2100 1000 6 2000 4200 6200
            "00013" "00105510" 2007 2300 1000 7 29500 4600 34100
            "00013" "00105510" 2008 2300 28500 7 29500 4600 34100
            "00013" "00105510" 2009 0 30400 8 61900 4800 66700
            "00013" "00105510" 2010 4800 31500 8 61900 4800 66700
            "00013" "00105510" 2011 5000 35800 9 60800 7500 68300
            "00013" "00105510" 2012 2500 25000 9 60800 7500 68300
            "00013" "00105510" 2013 0 34900 10 78700 5200 83900
            "00013" "00105510" 2014 5200 43800 10 78700 5200 83900
            "00013" "00105510" 2015 0 0 11 45900 2866 48766
            "00013" "00105510" 2016 2866 45900 11 45900 2866 48766
            "00013" "00105510" 2017 2700 42700 12 80680 5400 86080
            "00013" "00105510" 2018 2700 37980 12 80680 5400 86080
            "00013" "00105510" 2019 5500 41500 13 41500 5500 47000
            "00013" "00105510" 2020 0 0 13 41500 5500 47000
            "00013" "00105510" 2021 0 0 14 0 0 0
            "00125" "08467070" 1997 1000 0 2 0 2000 2000
            "00125" "08467070" 1998 1000 0 2 0 2000 2000
            "00125" "08467070" 1999 1000 1000 3 1300 3000 4300
            "00125" "08467070" 2000 2000 300 3 1300 3000 4300
            "00125" "08467070" 2001 0 0 4 2300 3000 5300
            "00125" "08467070" 2002 3000 2300 4 2300 3000 5300
            "00125" "08467070" 2003 4000 0 5 2300 6000 8300
            "00125" "08467070" 2004 2000 2300 5 2300 6000 8300
            "00125" "08467070" 2005 2000 2000 6 2000 14700 16700
            "00125" "08467070" 2006 12700 0 6 2000 14700 16700
            "00125" "08467070" 2007 9200 0 7 0 40000 40000
            "00125" "08467070" 2008 30800 0 7 0 40000 40000
            "00125" "08467070" 2009 7200 0 8 0 14400 14400
            "00125" "08467070" 2010 7200 0 8 0 14400 14400
            "00125" "08467070" 2011 43300 0 9 0 128300 128300
            "00125" "08467070" 2012 85000 0 9 0 128300 128300
            "00125" "08467070" 2013 42800 2600 10 2600 83000 85600
            "00125" "08467070" 2014 40200 0 10 2600 83000 85600
            "00125" "08467070" 2015 91500 0 11 0 169102 169102
            "00125" "08467070" 2016 77602 0 11 0 169102 169102
            "00125" "08467070" 2017 65600 0 12 0 138800 138800
            "00125" "08467070" 2018 73200 0 12 0 138800 138800
            "00125" "08467070" 2019 254100 0 13 0 254100 254100
            "01440" "26875P10" 1999 0 1000 3 1000 0 1000
            "01440" "26875P10" 2000 0 0 3 1000 0 1000
            "01440" "26875P10" 2001 0 0 4 0 0 0
            "01440" "26875P10" 2002 0 0 4 0 0 0
            "01440" "26875P10" 2003 0 1000 5 1000 0 1000
            "01440" "26875P10" 2004 0 0 5 1000 0 1000
            "01440" "26875P10" 2005 0 0 6 500 0 500
            "01440" "26875P10" 2006 0 500 6 500 0 500
            "01440" "26875P10" 2007 0 0 7 0 0 0
            "01440" "26875P10" 2008 0 0 7 0 0 0
            "01440" "26875P10" 2009 0 4800 8 17300 0 17300
            "01440" "26875P10" 2010 0 12500 8 17300 0 17300
            "01440" "26875P10" 2011 0 4250 9 15250 0 15250
            "01440" "26875P10" 2012 0 11000 9 15250 0 15250
            "01440" "26875P10" 2013 0 7600 10 7600 0 7600
            end
            
            bysort Person_ID count_cycles (Total_cycle) : gen donated = cond(_n == _N, Total_cycle > 0, 0)
            by Person_ID : egen wanted = total(donated)
            
            tabdisp Person_ID, c(wanted)
            
            ----------------------
            Person_ID | wanted
            ----------+-----------
            00013 | 12
            00125 | 12
            01440 | 6
            ----------------------
            
            .
            Many thanks Nick it works perfectly but I have more question please

            Can I create a variable that count the number of cycles in which the person donate only to Red Party,

            this happens only when "Total_red_each_cycle" = "Total_cycle"


            for instance for the first and the second persons, this variable will be equal to zero as they donate to both parties

            however for the third person the variable will be equal to 6 ( As he donated only to the Red party during the 6 cycles) , but in some other cases- I didn't post them- may be the person donated for to red party for only 1 or more cycle

            So do you have any ideas how to create that variable ?


            Last edited by Mohamed Mahmoud; 18 Dec 2022, 09:42.

            Comment


            • #7
              Originally posted by Carlo Lazzaro View Post
              Mohamed:
              have you already given a shot to:
              Code:
              . bysort Person_ID (Firm_ID): gen Count_donated_cycles= _N if Total_cycle!=0
              Many thanks Carlo for your help, Nick commands works perfectly , thank you again

              Comment


              • #8
                Code:
                 
                 bysort Person_ID count_cycles (Total_cycle) : gen donated = cond(_n == _N, Total_red_each_cycle == Total_cycle, 0)

                Comment

                Working...
                X