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 from long to wide and attaching Labels contained in a Variable.


From   Phil Schumm <[email protected]>
To   <[email protected]>
Subject   Re: st: Reshape from long to wide and attaching Labels contained in a Variable.
Date   Thu, 29 Aug 2013 18:04:08 -0500

On Aug 29, 2013, at 4:11 PM, Stefan Schöftner <[email protected]> wrote:
> I got a  sample of a Data file looking something like this:
>  
> Long FORM:
> ID´s repeat therefor i need the wide form.
>  
> Obs.          ID       Title             Amount                Code
> 1              xxx       abc              12345                   yy1
> 2              xxx       bcd              54321                   yy2
> 3              xxx       cda              43215                   yy3
> 4              xxx       acb              32154                   yy4
> 5              xxx       cad              21543                   yy4
>  
>  
> I use a reshape command to bring this dataset into wide form
> so it looks like this
>  
> Wide Form:
>  
> Obs.          ID        yy1      yy2    yy3    yy4      yy5        Title       
> 
> 1              xxx                                                  abc
> 2              xxx                                                  bcd
> 3              xxx      AMOUNT ---------------------Amount          cda
> 4              xxx                                                  acb
> 5              xxx                                                  cad
>  
> What I now desire, would be that the variable "Title" be converted into  the label text for the according variables yy1-yy5-yyx.
> yyx because the entire dataset consists of several 10.000 observations
> The variable "code" itself would be enough for identification but since im going to work a lot more with this sample the change in Label would help me quite a bit.


There's a lot left unsaid here, and that makes it difficult to give an adequate answer.  For example, are you referring to variable labels, or value labels?  Also, your example data are not sufficient to illustrate what you are trying to achieve.  Finally, are you really sure you want these data in wide format -- what would you do with them in wide format that you couldn't do with them in long format?

Be that as it may, here is an answer based on my best guess for what you are trying to do:


    reshape wide amount title, i(id) j(code) string
    foreach var of varlist amount* {
        loc code = subinstr("`var'","amount","",1)
        loc tvar title`code'
        lab var `var' "`=`tvar'[1]'"
        ren `var' `code'
        drop `tvar'
    }


-- Phil


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