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   Tue, 20 Aug 2013 16:42:10 +0100

Looking at your original example, it seems destined to confuse.

You gave examples of -type- as "a" "b" "c" and then said that they
were numeric characters, which to me still implies a string variable.

Looking at your report, it seems less than clear.  What does "didn't
work" mean? The Statalist FAQ explains that you should say exactly
what you typed and exactly what happened.

If those "numeric characters" are different numbers in a numeric variable, then

1. You should't need to convert them to string at all. (But -tostring-
is the usual command and -string()- is the usual function should that
really be necessary.)

2. -reshape wide- strikes me as a bad idea. You could end up with many
different variables, 800 more if what you say is correct. Unless you
know how you are going to handle them, you would be better off with a
long structure.

3. As I warned, variable names are likely to be a problem unless all
the numbers are integers.

Nick
[email protected]

On 20 August 2013 16:06, HAKAN USLU <[email protected]> wrote:

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

Nick Cox

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

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