Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: How to collape across two variables?


From   "Friedrich Huebler" <[email protected]>
To   [email protected]
Subject   Re: st: How to collape across two variables?
Date   Wed, 18 Jul 2007 11:56:45 -0400

Nurhan,

It is indeed possible to -collapse- by more than one variable. The
-by()- option accepts a list of variables.

. sysuse auto
. collapse mpg, by(rep78 foreign)
. clist, noobs

  rep78   foreign       mpg
      1  Domestic        21
      2  Domestic    19.125
      3  Domestic        19
      3   Foreign   23.3333
      4  Domestic   18.4444
      4   Foreign   24.8889
      5  Domestic        32
      5   Foreign   26.3333
      .  Domestic     23.25
      .   Foreign        14

In this example, rep78 stands for the household ID, foreign for the
type of expense, and mpg for the amount.

Another option would be to create new variables containing the
expenditures on food and clothing.

. sysuse auto, clear
. gen dom_mpg = mpg if foreign == 0
. gen for_mpg = mpg if foreign == 1
. collapse dom_mpg for_mpg, by(rep78)
. clist, noobs

  rep78    dom_mpg    for_mpg
      1         21          .
      2     19.125          .
      3         19   23.33333
      4   18.44444   24.88889
      5         32   26.33333
      .      23.25         14

rep78 stands for the household ID, dom_mpg for food expenses, and
for_mpg for clothing expenses. In this case the structure of the final
data set matches your example.

Friedrich

P.S. Do you have a version of Stata that accepts variable names that
do not begin with a letter or underscore?

On 7/18/07, dnurhan <[email protected]> wrote:
 My data set looks like:

 ID          $AMOUNT    TYPE
4001           $10                1=food
4001           $25                1=food
4001           $12                2=clothing
4001           $33                2=clothing
4002          ....                    ...
4002         ....                     ...

i would like to sum across both $AMOUNT  &  TYPE,  to get each family's
spending on food and clothing as

ID       FOOD     CLOTHING
4001    $35            $45
4002   ....              .....

  How do i do this?? (i am new to the list, looked at the manuals but they
explain using "collapse" across only one
                                    variable)

Best regards, nurhan
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index