Announcement

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

  • Adding missing years and carrying forward recent values without using carryfoward

    Dear all,
    I am working in stata 15. I have election data (https://ppeg.wzb.eu/www/ppeg_parl_2024v1.zip) from different countries (cname_en) and their parties (cmp or pname_en) and want to calculate the mean vote share (v_share) for right-wing populist parties across these countries and over time. The problem is that elections in different countries are only every 4 to 5 years. To calculate a mean, I have to first add observations for each country and each party in between elections. So if Party A in country B won 20% in 2015, until the next election in 2019 each year must display the vote share (v_share) from 2015. Additionally, for a party that does not gain any shares in a subsequent election (e.g. because they dissolved), the vote share from the previous election should not be carried forward

    My first code was looking somewhat like this:
    Code:
    cap drop year
    gen year = year(edate)
    fre year
    
    drop if year <2000
    keep cname_en cmp electorate total_vote valid_vote party_id pname_en v_share year
    
    * sorting dataset
    sort cname_en pname_en year
    
    * Create a new variable for the last year with data for each party in each country
    by cname_en pname_en (year): gen last_year = year[_N]
    
    * Expand the dataset to include all years for each party in each country
    expand last_year - year + 1
    
    * Create a sequence of years for each party in each country
    bysort cname_en pname_en (year): gen seq_year = year[1] + _n - 1
    
    * Replace the year variable with the sequence of years
    replace year = seq_year
    drop seq_year last_year
    
    drop if year > 2023
    The drop of years above 2023 was necessary because the code generated values past 2023.

    The problem is that for each party and year I would mostly just have one value and not the forwardcarried values of the past election. I tried also the carryforward command but it fails because each year appears several times.

    Maybe there is also another way to interpolate values that I am not aware of.

    I hope someone can help me with this! Also, it's my first post so if something is missing, let me know.

    Thanks in advance!
    Ramon

Working...
X