Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: Reshape Wide


From   "Taha Kass-Hout" <kasshout@capastatistic.com>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: Reshape Wide
Date   Sat, 14 Sep 2002 23:46:30 -0400

Hello -

When I try to reshape my data from long to wide I end up with far less
observations than there is.  I have total of 408 observations, instead I can
only get 207 after reshaping the data.  The long shaped data has the
following vars:

no              str3   %3s
date            str9   %9s
question        str14  %14s
answer          str80  %80s

Below is the do file I am running.  Any help would be greatly appreciated -
Thanks

Taha

*As data in "ans" contains almost just real numbers, I would split the
*process into two parts: First part, the real numbers take less memory than
strings.
*Second part, the strings.
*Then merge the two parts together.
*----------------------------------------
*For the first part generate a variable, which contains the contents
*of "ans" as a number:
*----------------------------------------
* First Part
* ----------
use survey.dta,clear /* Change to the name of your data */
d
gen v = real(ans)
*Then reshape this variable, and only the question, which has "real"
*numbers as answers:
drop ans
keep if v ~= .
*Reshape with the string option after changing the
*points in the question numbers into "_" (or something else).
replace question = subinstr(question,".","_",.)
reshape wide v, i(no) j(question) string
compress
sort no
save part1, replace
*----------------------------------------
* Second Part
* -----------
use survey, clear  /* Change to the name of your data */
*Reshape the "real"  string-data.
gen v = real(ans)
keep if v == .
drop v
ren ans v
replace question = subinstr(question,".","_",.)
reshape wide v, i(no) j(question) string
compress
sort no
save part2, replace
* Merge the two parts together
* ----------
use part1, clear
merge no using part2
assert _merge==3  /* Something is wrong if assertion is false */
drop _merge
aorder  /* Dont' like the order of vars? Use this or "order" */
save merge, replace
*---------------------------------------------------------------------------

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index