Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: RE: problem with -collapse- [was: ]


From   Nick Cox <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: st: RE: problem with -collapse- [was: ]
Date   Mon, 19 Aug 2013 20:04:51 +0100

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
[email protected]


On 19 August 2013 19:53, HAKAN USLU <[email protected]> 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/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index