Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


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

Re: st: Database problem


From   Nick Cox <njcoxstata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Database problem
Date   Sat, 8 Dec 2012 12:35:26 +0000

It sounds as if you need something like this

rename var1 id
rename var2 age
rename var3 sex
rename var4 foo
rename var5 bar
rename var6 frog

local max "<index of highest number variable>"

local i = 1
foreach j = 7(3)`max' {
        rename var`j'   number`i'
        local J = `j' + 1
        rename var`J'  diagnosis`i'
        local J = `J' + 1
        rename var`J' date`i'
        local ++i
}

gen long obsno = _n
reshape long id age sex foo bar frog number date diagnosis , i(obsno)

The loop isn't necessary, just convenient!

Some commands can be shortened in Stata 12. (See -help rename groups-.)

Nick

On Fri, Dec 7, 2012 at 7:16 PM, roland andersson <rolandersson@gmail.com> wrote:
> I have data on 3574 unique patients. In the datase there are first 5
> identification variables ,age, sex and so on. Thereafter comes a
> nomber of variables representing observations. Each observation has
> three columns - the sequential number of the observation, the content
> of the observation and the date. The problem is that these observation
> are in reverse order, ie the last observation comes first and the
> first observation last in the row. And the number of observations for
> each patient range from 1 to 44. Some observations have the same date
> so they have the same sequential number. below is the data. Var 7, var
> 10 and var 13 are the number of the observation, var 8, 11, and 14 the
> observation (the diagnosis or intervention) and var 9, 12 and 15 the
> date of the observation.
>
> I want to reverse the order so that the first observation comes in as
> var 7 etc. I also want to merge observations with the same date with a
> new combined content. case nr 1 below sholuld thus be 1
> diverticulitsigmo 15930. Is this possible?
>
> Regards
> Roland Andersson
>
>  var7 var8 var9 var10 var11 var12 var13 var14 var15
> 1 diverticulit 15930 1 sigmo 15930
> 1 akutiverticulit 16670 1 sigmo 16670
> 3 akutiverticulit 17126 2 akutiverticulit 16962 1 akutiverticulit 16359
> 2 akutiverticulit 17019 1 akutiverticulit 17019
> 1 akutiverticulit 15831 1 resektion 15831
> 1 akutiverticulit 16129
> 3 ctbuk 16839 2 sigmo 16838 1 akutiverticulit 16532
*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index