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: Reshaping but not numeric and more a loop needed!


From   Nick Cox <[email protected]>
To   "'[email protected]'" <[email protected]>
Subject   RE: st: Reshaping but not numeric and more a loop needed!
Date   Mon, 31 Oct 2011 10:40:39 +0000

Another way to approach this, at least for examples of similar size. The limit is not the number of rows in each table, but the width of the columns. Note that in this and in Scott's solution no loops are needed. 

. clear

. input  A       B

             A          B
  1. 1.11    1.11
  2. 1.11    1.12
  3. 1.11    1.13
  4. 1.11    1.14
  5. 1.11    1.15
  6. 1.11    1.16
  7. 1.11    1.19
  8. 1.11    1.26
  9. 1.11    1.28
 10. 1.11    1.29
 11. 1.11    1.63
 12. 1.11    1.64
 13. 1.20    1.11
 14. 1.20    1.12
 15. 1.20    1.20
 16. 1.20    1.20
 17. 1.21    1.13
 18. 1.21    1.14
 19. 1.21    1.21
 20. 1.21    1.22
 21. 1.21    1.23
 22. end

. gen B2 = string(B, "%3.2f")

. bysort A : replace B2 = B2[_n-1] + " " + B2  if _n > 1
(18 real changes made)

. bysort A : gen tag = _n == _N

. list A B2 if tag , noobs 

  +--------------------------------------------------------------------+
  |    A                                                            B2 |
  |--------------------------------------------------------------------|
  | 1.11   1.11 1.12 1.13 1.14 1.15 1.16 1.19 1.26 1.28 1.29 1.63 1.64 |
  |  1.2                                           1.11 1.12 1.20 1.20 |
  | 1.21                                      1.13 1.14 1.21 1.22 1.23 |
  +--------------------------------------------------------------------+

Nick 
[email protected] 

Scott Merryman

Something like this?

clear
input  A       B
1.11    1.11
1.11    1.12
1.11    1.13
1.11    1.14
1.11    1.15
1.11    1.16
1.11    1.19
1.11    1.26
1.11    1.28
1.11    1.29
1.11    1.63
1.11    1.64
1.20    1.11
1.20    1.12
1.20    1.20
1.20    1.20
1.21    1.13
1.21    1.14
1.21    1.21
1.21    1.22
1.21    1.23
end

bys A: gen tag = _n
egen tag2 = group(A)
reshape wide A B, j(tag) i(tag2)
rename A1 A
drop A? A?? tag2
l

Scott

On Sat, Oct 29, 2011 at 6:29 PM, Miyu Lee <[email protected]> wrote:

> I want to reshape the following table
>
> A           B
> 1.11    1.11
> 1.11    1.12
> 1.11    1.13
> 1.11    1.14
> 1.11    1.15
> 1.11    1.16
> 1.11    1.19
> 1.11    1.26
> 1.11    1.28
> 1.11    1.29
> 1.11    1.63
> 1.11    1.64
> 1.20    1.11
> 1.20    1.12
> 1.20    1.20
> 1.20    1.20
> 1.21    1.13
> 1.21    1.14
> 1.21    1.21
> 1.21    1.22
> 1.21    1.23
> etc     etc
>
> to look like this:
>
> A          B1       B2     B3
> 1.11    1.11     1.12    1.13 etc
> 1.20    1.11     1.12    1.20 etc
> 1.21    1.14 etc.
>
> !!! my ideal would be if STATA just puts the B value next to the B
> value of the first observation within each group, as long as the A
> values are the same.!!!
>
> reshape does not work here because it creates a huge matrix where all
> identical B values are stacked on top of each other and/or there might
> also be a problem that the size of the group is not equal.
>
> any suggestions would be helpful. the matrix is very long so a loop or
> anything similar seems the only alternative.
>

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


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