Statalist


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

Re: st: How to collape across two variables?


From   "Eric G. Wruck" <[email protected]>
To   [email protected]
Subject   Re: st: How to collape across two variables?
Date   Wed, 18 Jul 2007 11:48:02 -0400

You were on the right track w. collapse.  Here's how you could make it work:

. l

     +----------------------+
     |   id   amount   type |
     |----------------------|
  1. | 4001       10      1 |
  2. | 4001       25      1 |
  3. | 4001       12      2 |
  4. | 4001       33      2 |
     +----------------------+

. sort id type

. collapse (sum) amount, by(id type)

. l

     +----------------------+
     |   id   type   amount |
     |----------------------|
  1. | 4001      1       35 |
  2. | 4001      2       45 |
     +----------------------+

But since you seem to want the expenditures for an id on a single line (observation), you'll want to reshape the data:

. reshape wide amount, i(id) j(type)
(note: j = 1 2)

Data                               long   ->   wide
-----------------------------------------------------------------------------
Number of obs.                        2   ->       1
Number of variables                   3   ->       3
j variable (2 values)              type   ->   (dropped)
xij variables:
                                 amount   ->   amount1 amount2
-----------------------------------------------------------------------------

. l

     +--------------------------+
     |   id   amount1   amount2 |
     |--------------------------|
  1. | 4001        35        45 |
     +--------------------------+


Here you'll probably want to rename the amount variables to food & clothing.

Eric


>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
>


-- 

===================================================

       Eric G. Wruck
       Econalytics
       2535 Sherwood Road
       Columbus, OH  43209

       ph:      614.231.5034
       cell:    614.330.8846
       eFax:    614.573.6639
       eMail:   [email protected]
       website: http://www.econalytics.com

====================================================
*
*   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