Announcement

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

  • How to generate a variable with the total number per year only?

    Good day all,

    I have a dataset with 3 variables: (survival status "survived/died", year, number of observations). Now I would like to create a new variable which is basically the sum of the observations per year whilst ignoring the survival status. The reason I am trying to do this is because I would like to estimate the percentage later on based on the survival status (i.e. by dividing the number of observations per survival status by the total number of observations per year). I tried this command:

    Code:
    by year: gen tot = sum(num)
    But it did not work

    Any suggestions? Cheers!

  • #2
    If I understand correct, you can use egen and do

    egen newvar = count(var) by(year)

    if you need an if condition these can work also

    https://www.stata.com/manuals13/degen.pdf

    Comment


    • #3
      Ibby:
      you may want to try something along the lines of the following toy-example:
      Code:
      . use "http://www.stata-press.com/data/r15/nlswork.dta", clear
      (National Longitudinal Survey.  Young Women 14-26 years of age in 1968)
      
      . bysort year: egen newvar = count( idcode )
      
      . tab newvar
      
           newvar |      Freq.     Percent        Cum.
      ------------+-----------------------------------
             1232 |      1,232        4.32        4.32
             1375 |      1,375        4.82        9.14
             1686 |      1,686        5.91       15.05
             1693 |      1,693        5.93       20.98
             1847 |      1,847        6.47       27.45
             1851 |      1,851        6.49       33.94
             1964 |      1,964        6.88       40.82
             1981 |      1,981        6.94       47.76
             1987 |      1,987        6.96       54.73
             2085 |      4,170       14.61       69.34
             2141 |      2,141        7.50       76.85
             2164 |      2,164        7.58       84.43
             2171 |      2,171        7.61       92.04
             2272 |      2,272        7.96      100.00
      ------------+-----------------------------------
            Total |     28,534      100.00
      
      .
      Kind regards,
      Carlo
      (Stata 18.0 SE)

      Comment


      • #4
        In the future please do not say "it did not work." Explain exactly what happened.

        Comment

        Working...
        X