Title | Stata 5: Avoiding “too many variables” when using reshape | |
Author | James Hardin, StataCorp |
You type
. reshape groups year 90-95 . reshape vars inc . reshape cons id sex age race hgt wgt shoesize hatsize ms kids addr too many variables r(103);
reshape cons allows a maximum of 10 variables to be specified, and there are 11 in the above example. This problem has been resolved in the updated version of reshape. The new version of reshape also has a simpler syntax and other nice features. In addition, the new version of reshape understands the old reshape syntax, so your prior do-files and ado-files will still work.
Click here to learn about how to obtain the updated version of reshape.
Nevertheless, here is how to work around the limitation in the old version of reshape:
. use yourdata . keep id sex age race hgt wgt shoesize hatsize ms kids addr . sort id . quietly by id: keep if _n==1 . save demogs, replace
The quietly by id: keep if _n==1 is necessary only if your data are in the long form, but it will not hurt in any case.
In the above example, we assume that variable id is enough to uniquely identify each observation. If two variables are required (e.g., hospital-id and patient-id), substitute those two variable names for id.
. reshape groups year 90-95 . reshape vars inc . reshape cons id . reshape wide or reshape long
That is, reshape normally, but note the shorter reshape cons statement.
. sort id . merge id using demogs . keep if _merge==3 . drop _merge
In these lines, we merge back in the nonvarying characteristics from demogs.dta.
The keep if _merge==3 is not really necessary, but we recommend it. In the solution as given, keep if _merge==3 will do nothing because _merge must be 3. If you form demogs.dta one day, however, and then reshape on a subset of your data another day, the keep if _merge==3 is important.
That is all there is to dealing with dealing with this problem.
One of the steps performed internally by reshape is a match merge, using the reshape cons variables to match observations. While Stata allows you to sort on any number of variables, the maximum number of key variables in a match merge is 10. Hence, the limitation.
The reshape cons variables include those variables that (1) uniquely identify the subjects, and (2) do not vary within subject but that you want carried along. To fix the problem on our end, we should change reshape so that list (1) is given with some new reshape id command, which would continue to be limited to 10 variables—and list, and (2) is given by reshape cons and is not limited.
Learn
Free webinars
NetCourses
Classroom and web training
Organizational training
Video tutorials
Third-party courses
Web resources
Teaching with Stata
© Copyright 1996–2024 StataCorp LLC. All rights reserved.
×
We use cookies to ensure that we give you the best experience on our website—to enhance site navigation, to analyze usage, and to assist in our marketing efforts. By continuing to use our site, you consent to the storing of cookies on your device and agree to delivery of content, including web fonts and JavaScript, from third party web services.
Cookie Settings
Last updated: 16 November 2022
StataCorp LLC (StataCorp) strives to provide our users with exceptional products and services. To do so, we must collect personal information from you. This information is necessary to conduct business with our existing and potential customers. We collect and use this information only where we may legally do so. This policy explains what personal information we collect, how we use it, and what rights you have to that information.
These cookies are essential for our website to function and do not store any personally identifiable information. These cookies cannot be disabled.
This website uses cookies to provide you with a better user experience. A cookie is a small piece of data our website stores on a site visitor's hard drive and accesses each time you visit so we can improve your access to our site, better understand how you use our site, and serve you content that may be of interest to you. For instance, we store a cookie when you log in to our shopping cart so that we can maintain your shopping cart should you not complete checkout. These cookies do not directly store your personal information, but they do support the ability to uniquely identify your internet browser and device.
Please note: Clearing your browser cookies at any time will undo preferences saved here. The option selected here will apply only to the device you are currently using.