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: collapsing in a way to display the different values for a variable in a row


From   Maarten Buis <[email protected]>
To   [email protected]
Subject   Re: st: collapsing in a way to display the different values for a variable in a row
Date   Wed, 5 Feb 2014 13:46:59 +0100

On Wed, Feb 5, 2014 at 1:26 PM, Raffaele Palladino wrote:
> I have a huge dataset where one of the variables lists different kind of medication ("treatment", string variable). Each person can take different medication so for every value of the variable "idperson" I have several records, each with a different value of the variable "treatment". Is there a way, maybe using collapse to have for each id person as much date and treatment variable as the real records for each idperson are?

Yes, the command is not called collapse but -reshape-, see -help
reshape- and the example below:

*------------------ begin example ------------------
// create some example data
clear
input ///
idperson  str15 strdate   str1 treatment
1         01February2000  a
1         15March2001     b
1         01January1999   c
2         02February2011  d
2         15September2001 e
2         01January1955   a
2         02January1956   b
3         03January1956   c
end

// turn the string dates into Stata dates
gen date = date(strdate, "DMY")
format date %td
drop strdate

// reshape the data
bys id (date) : gen idtreat = _n
reshape wide date treatment, i(idperson) j(idtreat)
*------------------- end example -------------------
* (For more on examples I sent to the Statalist see:
* http://www.maartenbuis.nl/example_faq )

Hope this helps,
Maarten

---------------------------------
Maarten L. Buis
WZB
Reichpietschufer 50
10785 Berlin
Germany

http://www.maartenbuis.nl
---------------------------------

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