Announcement

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

  • Generating variable which contains the share of observations in VTCS for each year

    Hello,


    I have data which looks like this:

    Year VTCS
    2009 0
    2009 0
    2009 1
    2009 0
    2010 1
    2010 1
    2010 0
    2010 0
    2011 0
    2011 0
    2011 0
    2011 0

    I want to generate a new variable indicating the percentage of observations that are 'VTCS' ovservations in each year i.e VTCS==1. So for example since in 2009 25% of observations are VTCS observations (1/4) then the variable would take the value 25 for all observations in 2009.

    So the new data would look like this :
    Year VTCS New Variable
    2009 0 25
    2009 0 25
    2009 1 25
    2009 0 25
    2010 1 50
    2010 1 50
    2010 0 50
    2010 0 50
    2011 0 0
    2011 0 0
    2011 0 0
    2011 0 0

    How can I implement something like this?

    Essentially I want to plot a twoway graph with the share of observations that are VTCS on the y axis, and year on the x axis.

    Thanks,
    Jad










  • #2
    Code:
    assert inlist(VTCS, 0, 1)
    by year, sort: egen wanted = mean(VTCS)
    replace wanted = 100*wanted
    In the future, when showing data examples, please use the -dataex- command to do so. If you are running version 18, 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.
    Last edited by Clyde Schechter; 07 Mar 2024, 18:38.

    Comment

    Working...
    X