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: Reshape question


From   Nick Cox <[email protected]>
To   [email protected]
Subject   Re: st: Reshape question
Date   Sun, 8 May 2011 08:38:55 +0100

The answer to Mike's question is documented.

FAQ     . . . . . . . . . . . . . . . . . . . . . . . .  Problems with reshape
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
        12/03   I am having problems with the reshape command. Can
                you give further guidance?
                http://www.stata.com/support/faqs/data/reshape3.html

% begn quotation

Question
If I have many variables all occurring in pairs for two years 1997 and
1998, so that the dataset looks like A97, A98, B97, B98, and so on, is
there any easy way to reshape the data to long without typing all the
stub names?
Answer
The variable names are collectively *97 *98, so we need a way of
expanding that list of wildcard names automatically and then removing
the suffix. We can work on either *97 or *98.

unab is usually billed as a programmer's command, but it can be used
interactively. It unabbreviates a varlist and puts the result in a
local macro.

  . unab vars : *97

Then we zap all the occurrences of the suffix "97":

  . local stubs : subinstr local vars "97" "", all

In other words, each occurrence of "97" is replaced by an empty
string; that is, they are removed. See macro.

Then we can

  . reshape long `stubs', options

% end quotation.

So in this case:

  . unab vars : a_*
  . local stubs : subinstr local vars "a_" "", all

So Keith is right, you can construct the stubs as a local macro. But
you certainly don't need to type them all out.

Nick

On Sun, May 8, 2011 at 1:00 AM, Keith Dear <[email protected]> wrote:
> Mike,
> Not that I know of. You might be able to build the list as a macro,
> using a loop that exploits the structure of the 70 names. But really,
> 70 isn't so very many -- why not just list them explicitly, like this:
>
> loc mylist gg hh ii             // etc etc
> loc mylist = subinstr(" `mylist'"," "," @_",.)          // nb leading spaces
> reshape long `mylist', i(id) j(country) string
>
> Keith
>
> On 7 May 2011 22:22, Mike Kim <[email protected]> wrote:
>> Keith Dear,
>>
>> Thank you, it works! Additional question is... my data contain around 70
>> different variable names with the same pattern (so, the total variables are
>> 70*5 = 3500). Is there any way I can reshape without listing all 70 variable
>> names?
>>
>> Mike.
>>
>> -----Original Message-----
>> From: [email protected]
>> [mailto:[email protected]] On Behalf Of Keith Dear
>> Sent: Saturday, May 07, 2011 1:02 AM
>> To: [email protected]
>> Subject: Re: st: Reshape question
>>
>> reshape long @_gg @_hh @_ii , i(id) j(country) string
>>
>>
>> On 7 May 2011 14:00, Mike Kim <[email protected]> wrote:
>>> Dear listers,
>>>
>>> My data looks like the following and I want to reshape it from wide to
>> long.
>>> There are many more similar variables with the same pattern. Is there any
>>> easy way to reshape?
>>>
>>> id a_gg b_gg c_gg d_gg e_gg  a_hh b_hh c_hh d_hh e_hh  a_ii b_ii c_ii d_ii
>>> e_ii   ..
>>> 1     1        5        3        1      2        4        1        1
>>> 5         1        3      1      1     4      1
>>> 2    4        1        1        2       1        1        1        2
>>> 1         1         1      1      5     1      1
>>> 3    2        1        2        1       1        1        5        1
>>> 1         3         1      1      1     3      1
>>> ..
>>>
>>> The data format I want is:
>>>
>>> id country gg  hh  ii  ..
>>> 1     a
>>> 1     b
>>> 1     c
>>> 1     d
>>> 1     e
>>> 2     a
>>> 2     b
>>> 2     c
>>> 2     d
>>> 2     e
>>> 3     a
>>> 3     b
>>> 3     c
>>> 3     d
>>> 3     e
>>> ......
>>>

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