Announcement

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

  • Contract command

    I need to calculate the frequencies of a categorical variable across different time periods. I want to create a new data set of those frequencies. I was trying to use the "contract" command, but it does not have the by option. Can anybody suggest something. Can I use "collapse" for this? Any help would be appreciated.

  • #2
    I guess so:
    Code:
    clear
    set more off
    
    *----- example data -----
    
    sysuse auto
    keep rep78 foreign
    
    // fails
    capture bysort foreign : contract rep78
    list
    
    // works
    gen rep78_2 = rep78
    collapse (count) count=rep78, by(rep78_2 foreign)
    
    // list
    sort foreign rep78_2
    list, sepby(foreign)
    Watch out for the missings.

    There are other ways too.
    You should:

    1. Read the FAQ carefully.

    2. "Say exactly what you typed and exactly what Stata typed (or did) in response. N.B. exactly!"

    3. Describe your dataset. Use list to list data when you are doing so. Use input to type in your own dataset fragment that others can experiment with.

    4. Use the advanced editing options to appropriately format quotes, data, code and Stata output. The advanced options can be toggled on/off using the A button in the top right corner of the text editor.

    Comment


    • #3
      If you want

      Code:
       
      bysort avar : contract bvar
      then

      Code:
       
      contract avar bvar
      is surely the same thing, as it produces frequencies according to cross-combinations of the variables; and so too more generally for varlists in each position. Otherwise put, as the original author of contract I must have missed something if you want something else.

      Comment


      • #4
        Thanks everyone. Nick's suggestion worked perfectly.

        Comment

        Working...
        X