Statalist The Stata Listserver


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

Re: st: -reshape-, or "is there an easier way to reshape my data than how I am doing it?"


From   "Sauermann, Jan" <[email protected]>
To   "StataList (E-Mail)" <[email protected]>
Subject   Re: st: -reshape-, or "is there an easier way to reshape my data than how I am doing it?"
Date   Tue, 25 Jul 2006 09:07:04 +0200

I had a similar problem before. If you use John's suggestion:

collapse (max) gbm-gwf,by(year fip age)

you probably will have missings in your "reshaped" datasets, because "." is larger than (e.g.) "3":

      +--------------------------------------------------------+
      | year   fip   age       gbm       gbf      gwm      gwf |
      |--------------------------------------------------------|
   1. | 1983     1    15    data        .        .        . |
   2. | 1983     1    15         .    data        .        . |
   3. | 1983     1    15         .         .    data        . |
   4. | 1983     1    15         .         .        .      data |

    +---------------------------------------------------------------+
      | year   fip   age       gbm       gbf      gwm      gwf     |
      |----------------------------------------------------------------|
   1. | 1983     1    15    	.        	.	    .		. 	 |


 So, if your "data" are numbers only, and all other values in the respective column are missings, you should type

collapse (min) gbm-gwf,by(year fip age)

Then, the result should look like: 

    +---------------------------------------------------------------+
      | year   fip   age       gbm       gbf      gwm      gwf     |
      |----------------------------------------------------------------|
   1. | 1983     1    15    data        data      data      data  |



Jan



--

---------------------------------------
Jan Sauermann

Halle Institute for Economic Research (IWH)

Kleine Maerkerstrasse 8 | phone: +49 345 7753-855 
D - 06108 Halle (Saale) | fax:   +49 345 7753-799
http://www.iwh-halle.de | mail:  [email protected]
http://www.iwh-halle.de/d/Abteil/VWGR/jsa/pers.htm




Maybe I misunderstand the question but this is doable by collapse:

collapse (max) gbm-gwf,by(year fip age)

assuming you're going from this:
      +--------------------------------------------------------+
      | year   fip   age       gbm       gbf      gwm      gwf |
      |--------------------------------------------------------|
   1. | 1983     1    15    data        .        .        . |
   2. | 1983     1    15         .    data        .        . |
   3. | 1983     1    15         .         .    data        . |
   4. | 1983     1    15         .         .        .      data |
to this:
    +---------------------------------------------------------------+
      | year   fip   age       gbm       gbf      gwm      gwf     |
      |----------------------------------------------------------------|
   1. | 1983     1    15    data        data      data      data  |

John.

Scott Cunningham wrote:
> I'm having a hard time figuring out how to use reshape with the data 
> in the form I currently have it in.
>
> DATA:  1982-2004 CDC data on gonorrhea rates by age cohort, race, sex 
> and state
>
>       +--------------------------------------------+
>        | year   fip   race   sex        gr   age |
>        |---------------------------------------------|
>
> where:
>
> ------------------------------------------------------------------------------- 
>
> year            int    %8.0g                  YEAR
> fip             byte   %8.0g                  State FIPS codes
> race            byte   %8.0g                  W=1, B=2
> sex             byte   %8.0g                  M=1, F=2
> gr              float  %9.0g                  Gonorrhea Rate (per 
> 100,000)
> age             float  %9.0g                  Age Cohort
> ------------------------------------------------------------------------------- 
>
>
> I need to use -reshape- to transform the data into a format that will 
> allow me to estimate various panel models.  I am going to merge other 
> demographic data to this dataset, but that I can do.  What I cannot do 
> is figure out how to get -reshape- to work here.  When I simply use 
> -reshape-, I am told that I have fip/year that do not uniquely 
> identify.  I know this, of course.  So, I thought that what I could do 
> was create a race, gender and state specific gonorrhea rate.  So I did 
> that.  It looked like this:
>
>      +-------------------------------------------------------------+
>       | year   fip   age       gbm       gbf      gwm      gwf |
>       |--------------------------------------------------------------|
>
> where gbm is "gonorrhea rates for black males," gbf for black females, 
> gwm for white males, and gwf for white females.  I then -preserve- the 
> data, keep only the one of the age cohorts (15-19 year olds 
> initially), drop the age variable, and then try to -reshape- again.  
> But this doesn't work, because I have four fip observations for every 
> year, that looks like this:
>
>       +--------------------------------------------------------+
>       | year   fip   age       gbm       gbf      gwm      gwf |
>       |--------------------------------------------------------|
>    1. | 1983     1    15    data        .        .        . |
>    2. | 1983     1    15         .    data        .        . |
>    3. | 1983     1    15         .         .    data        . |
>    4. | 1983     1    15         .         .        .      data |
>
> I need to collapse these observations so that it looks like this:
>
>       +---------------------------------------------------------------+
>       | year   fip   age       gbm       gbf      gwm      gwf     |
>       |----------------------------------------------------------------|
>    1. | 1983     1    15    data        data      data      data  |
>
> But neither -fillin- nor -collapse- appear to do the trick.
>
> MY QUESTION:  How do I collapse the data like the above?  Secondly, is 
> there a more efficient way to take my data in its original format and 
> create a panel without having to create 9 different age-specific 
> panels which I will merge later?*
> *   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/
*
*   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/

--

---------------------------------------
Jan Sauermann

Halle Institute for Economic Research (IWH)

Kleine Maerkerstrasse 8 | phone: +49 345 7753-855 
D - 06108 Halle (Saale) | fax:   +49 345 7753-799
http://www.iwh-halle.de | mail:  [email protected]
http://www.iwh-halle.de/d/Abteil/VWGR/jsa/pers.htm


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