Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | HAKAN USLU <hakan_uslu84@hotmail.com> |
To | "statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu>, "njcoxstata@gmail.com" <njcoxstata@gmail.com> |
Subject | RE: st: RE: problem with -collapse- [was: ] |
Date | Tue, 20 Aug 2013 15:06:11 +0000 |
Nick, I tried your command below but it did not work for my data. do you know how to convert all numeric characters into string characters in -type-? thank you Hakan ---------------------------------------- > Date: Mon, 19 Aug 2013 20:04:51 +0100 > Subject: Re: st: RE: problem with -collapse- [was: ] > From: njcoxstata@gmail.com > To: statalist@hsphsun2.harvard.edu > > Try this > > contract * > list > reshape wide _freq , i(year month) j(type) string > renpfix _freq > list > mvencode * , mv(0) > list > > That said, my wild guess is that the -reshape- may cause more problems > than it solves. Also, watch out for characters in -type- that can't > appear in variable names. > > > Nick > njcoxstata@gmail.com > > > On 19 August 2013 19:53, HAKAN USLU <hakan_uslu84@hotmail.com> wrote: >>> Hello all, >>> I am having a problem with -collapse (count)- >>> Can someone help me to convert (collapsing and counting) my data as >>> follows? ; >>> >>> My data looks like this: >>> year month type >>> 2010 1 a >>> 2010 1 a >>> 2010 1 b >>> 2011 1 b >>> 2012 3 b >>> 2010 1 c >>> 2011 4 c >>> 2011 4 c >>> 2011 4 c >>> 2010 2 c >>> 2012 5 c >>> 2012 5 c >>> >> >>> I need data that looks like this: >>> >>> year month a b c >>> 2010 1 2 1 1 >>> 2010 2 0 0 1 >>> 2011 1 0 1 0 >>> 2011 4 0 0 3 >>> 2012 3 0 0 0 >>> 2012 5 0 0 2 >>> >>> >>> Basically, I try to collapse and count my data with respect to month >>> and year. I have 800 different numeric characters in the variable >>> namely 'type'. I want to convert each character to a new variable. I >>> use Stata 11.0. I already tried the following loop but I got unexpected >>> error message like 'type mismatch' or 'no observations'. >>> >>> foreach c in 1/800 { >>> preserve >>> keep if type == `c' >>> collapse (count) type, by (month year) >>> ren type `c' >>> save `c', replace >>> restore >>> } > * > * For searches and help try: > * http://www.stata.com/help.cgi?search > * http://www.stata.com/support/faqs/resources/statalist-faq/ > * http://www.ats.ucla.edu/stat/stata/ * * For searches and help try: * http://www.stata.com/help.cgi?search * http://www.stata.com/support/faqs/resources/statalist-faq/ * http://www.ats.ucla.edu/stat/stata/