Announcement

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

  • How to tabulate to variables depending on the year?

    Dear all,

    I'm a beginner in Stata and hope not to upset you with my problem. Probably the title of this question is wrong, but it's hard for me to verbalize the issue.
    My dataset contains inter alia two variables for the year of birth (y_birth) and the year of death (y_death).
    For example:
    person y_birth y_death
    William 1900 1975
    Steve 1905 1980
    I want to create a table in Stata that shows me for each year from 1900 to 2000 the total amount of births and deaths.

    I would really appreciate your support.

    Thanks and best regards

    Daniel

  • #2
    See the tabulate or contract commands to get frequencies. You may want to contract on either variable and not both.

    Comment


    • #3
      Thanks Dave. But I want to combine frequencies in one table. And the values might differ.
      What I want is to create a table like this:

      Year Births Deaths
      1950 1000 0
      1978 800 200
      1990 0 350

      Comment


      • #4
        Code:
        clear 
        input str7 person    y_birth    y_death
        William    1900    1975
        Steve    1905    1980
        end 
        reshape long y_, i(person) j(event) string 
        
        rename y_ year 
        
        tab year event 
        
        
                   |         event
              year |     birth      death |     Total
        -----------+----------------------+----------
              1900 |         1          0 |         1 
              1905 |         1          0 |         1 
              1975 |         0          1 |         1 
              1980 |         0          1 |         1 
        -----------+----------------------+----------
             Total |         2          2 |         4

        Comment


        • #5
          Hi Nick, thank you very much for your support! Do you have an idea how to do it without the column "Total"?
          But the row "Total" is good!

          Comment


          • #6
            Here's an alternative approach using -contract- to get births and deaths and a -merge- to put them together.

            Code:
            // make fake data
            clear
            set obs 1000
            gen yb = round(runiform(1,100)+1900,1)
            gen yd = round(runiform(1,100)+1900,1)
            save mydata.dta, replace
            
            // create birth frequencies
            contract yb, freq(births)
            rename yb year
            save births.dta, replace
            
            // create death frequencies
            use mydata.dta, clear
            contract yd, freq(deaths)
            rename yd year
            save deaths.dta, replace
            
            // merge birth and death data by year
            use births.dta, clear
            merge 1:1 year using deaths.dta
            drop _merge
            
            list in 1/10, clean
            Example output:


            Code:
                   year   births   deaths  
              1.   1901        3        4  
              2.   1902        6        9  
              3.   1903        8       12  
              4.   1904       15       10  
              5.   1905        8       10  
              6.   1906       10       12  
              7.   1907       10       15  
              8.   1908       12       12  
              9.   1909       12        7  
             10.   1910        9       10

            Comment


            • #7
              This will be seen to suppress totals.

              Code:
              clear 
              input str7 person    y_birth    y_death
              William    1900    1975
              Steve    1905    1980
              end 
              reshape long y_, i(person) j(event) string 
              
              rename y_ year 
              
              bysort year event : gen freq = _N 
              tabdisp year event, c(freq) 
              
              tab year event

              Comment


              • #8
                Thanks Dave and Nick. You helped a lot!
                Is it possible to implement the same table but keep the row "Total" at the end?

                Comment


                • #9
                  #4 code produced a table with Total and #7 code produced a table without, so I don't know what new you are asking.

                  Comment


                  • #10
                    Sorry for confusing you.
                    Yeah #4 produced a table with Total by introducing a new column and a new row. I only need the row total and want to suppress the column...

                    Comment


                    • #11
                      I'm lost. Please give an example of the output you want. Also, explore tabulate and tabdisp and related commands for yourself.

                      Comment


                      • #12
                        If you add the line of code,
                        Code:
                        generate total = births + deaths
                        you will get the total number of births and deaths by year.
                        Code:
                        . list in 1/10, clean
                        
                               year   births   deaths   total  
                          1.   1901        7        5      12  
                          2.   1902       10        9      19  
                          3.   1903       17       11      28  
                          4.   1904       12        8      20  
                          5.   1905       18       12      30  
                          6.   1906        4        9      13  
                          7.   1907        3        9      12  
                          8.   1908       11        8      19  
                          9.   1909        8       11      19  
                         10.   1910       14       10      24

                        Dave

                        Comment

                        Working...
                        X