Announcement

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

  • How to get a table that count the frequency and percentage of two variables by year

    Hello!

    I have a dataset with the following variables: Year, Symbol (to identify which firm it is), and the two variables I want to count frequency and percentage: voluntary and mandatory, they are both dummy variables. I want to count the frequency and percentage of two variables by year and put them in one table. This is what I did, but that doesn't count percentage, and the two variables can't be tabled together.
    Code:
    tab Year, c(count mandatory_prop)
    Please kindly give me some advice. Thanks a lot!

  • #2
    Code:
    webuse grunfeld, clear
    set seed 09062021
    forval i=1/2{
        gen dummy`i'= runiformint(0,1)
    }
    *START HERE
    frame put dummy1 dummy2 year, into(table)
    frame table{
        gen freq=1
        collapse dummy1 dummy2 (sum) freq, by(year)
        forval i=1/2{
            replace dummy`i'= dummy`i'*100
            label var dummy`i' "dummy`i' (%)"
       }
       lab var freq ""
       tabdisp year, cell(dummy1 dummy2 freq)
    }
    frame drop table
    Res.:

    Code:
    .    tabdisp year, cell(dummy1 dummy2 freq)
    
    ----------------------------------------------
         year | dummy1 (%)  dummy2 (%)        freq
    ----------+-----------------------------------
         1935 |         20          50          10
         1936 |         40          50          10
         1937 |         80          20          10
         1938 |         50          30          10
         1939 |         80          40          10
         1940 |         40          70          10
         1941 |         40          50          10
         1942 |         30          60          10
         1943 |         50          50          10
         1944 |         50          50          10
         1945 |         30          30          10
         1946 |         30          60          10
         1947 |         70          80          10
         1948 |         50          60          10
         1949 |         80          40          10
         1950 |         60          20          10
         1951 |         50          20          10
         1952 |         50          70          10
         1953 |         30          30          10
         1954 |         40          70          10
    ----------------------------------------------

    Comment


    • #3
      Thanks! It works perfectly!

      Comment

      Working...
      X