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 to wide but to particular variables


From   Eric Booth <[email protected]>
To   [email protected]
Subject   Re: st: Reshape to wide but to particular variables
Date   Sat, 21 Apr 2012 09:23:06 -0500

<>

The challenge here is managing the sort of the dataset so that it matches your outcome data.  Here's one way to do it:
******************!
clear
inp str9 Date str1	id	str4(clinic	activity)					
"01.01.01"	1	RC	A					
"01.01.01"	1	RC	B					
"01.01.01"	1	RC	D					
"02.01.01"	1	RC	A					
"02.01.01"	1	RC	C					
"02.01.01"	1	RC	E					
"02.01.01"	1	RC	F					
"01.01.01"	2	MR	A					
"01.01.01"	2	MR	C					
"01.01.01"	2	MR	D					
"01.01.01"	2	MR	F	
end
g sort = _n
egen g = group(Date clinic id)
bys g (sort): g i = string(_n)
drop sort g

reshape wide activity,  ///
	    i(Date id clinic) j(i) string

rename activity1 AorBorC1
rename activity2 AorBorC2
rename activity3 AcorBorC3
rename activity4 DorEorF1						
list
******************!

- Eric

__
Eric A. Booth
Public Policy Research Institute
Texas A&M University
[email protected]
Office: +979.845.6754


On Apr 21, 2012, at 4:08 AM, Paul O'Brien wrote:

> I have a database of clinical activity that I want to reshape to wide but to particular variables, but so far have failed.
> 
> Date	id	clinic	activity					
> 01.01.01	1	RC	A					
> 01.01.01	1	RC	B					
> 01.01.01	1	RC	D					
> 02.01.01	1	RC	A					
> 02.01.01	1	RC	C					
> 02.01.01	1	RC	E					
> 02.01.01	1	RC	F					
> 01.01.01	2	MR	A					
> 01.01.01	2	MR	C					
> 01.01.01	2	MR	D					
> 01.01.01	2	MR	F	
> 
> And what I want is:
> 				
> Date	id	clinic	AorBorC1	AorBorC2	AorBorC3	DorEorF1	DorEorF2	DorEorF3
> 01.01.01	1	RC	A	B		D		
> 02.01.01	1	RC	A	C		E	F	
> 01.01.01	2	MR	A	C		D	F	
> 
> Thanks,
> 
> Paul
> 
> 
> 
> 					
> 
> 
> *
> *   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/


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