Announcement

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

  • Counting

    Hello Stata folks

    I have been logging data on how politicians vote inside of parliament. The id the the name of that person. The L colomns stand for the legislation number. So L1 equals legislation number 1 and what that person voted. There are 5 options:" Fraværende" (absent), "For" (for), "Imod" (against), "Hverken for eller imod" (Neither for or against), or missing data (coded as .)

    I want to know how many times each politician (so the specific person/politician) voted "For" "Imod" "Hverken for eller imod" "Fraværende" and the total amount of times they voted. What is the easiest way to do this?

    I tried with egen and contracts commands, but I didn't really get anywhere. I looked for solutions online and on Youtube, but didn't find any workable solutions. Any ideas?

    Click image for larger version

Name:	eg1.png
Views:	1
Size:	10.1 KB
ID:	1711058


    Thanks in advance!

    /Jacob

    Attached Files

  • #2
    Try this:
    Code:
    reshape long L, i(id party) j(law_number)
    rename L how_voted
    drop if how_voted == "."
    collapse (count) frequency = law_number, by(id how_voted)
    tempfile holding
    save `holding'
    collapse (sum) frequency, by(id)
    gen how_voted = "Total"
    append using `holding'
    sort id how_voted
    Because you posted your example data in an unusable format (screenshot), the above code is not tested. The gist of it is correct, but it may contain typos or other errors. If it does not work and you need additional assistance, when posting back be sure to include example data using the -dataex- command. If you are running version 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.

    When showing example data, always use -dataex-.
    Last edited by Clyde Schechter; 24 Apr 2023, 12:28.

    Comment

    Working...
    X