Title | Keeping the same variable with collapse | |
Author | Nicholas J. Cox, Durham University, UK |
I frequently use collapse on my datasets and find it frustrating that I lose all my variable labels as they are replaced by something like (mean) varname. Is it possible to save the old variable labels so that they can be attached to the new variables?
collapse replaces the dataset in memory with a new dataset of group statistics. As reported, it has its own idea of suitable variable labels for the new variables.
However, it is easy to save your variable labels before collapse so that they can be used afterwards, with or without modification.
A systematic way to do this is with a foreach loop.
. foreach v of var * { . local l`v' : variable label `v' . if `"`l`v''"' == "" { . local l`v' "`v'" . } . }
What this does, for each variable in the dataset, is to copy its variable label to a local macro. If there is no variable label, we use the variable name instead. For more information on the commands used, see [P] foreach and [P] macro, or see the tutorial in Cox (2002).
* is a wildcard for all variables in the current dataset; for other ways of abbreviating variable lists, see [U] 11.4.1 Lists of existing variables.
In the simplest case, the new variables all have the same names as their originals. After collapse, you can then just use the old labels:
. foreach v of var * { . label var `v' `"`l`v''"' . }
This relabeling must be done in the same session as the collapse, as local macros do not survive beyond the end of a session.
Normally, it should matter little if the new dataset contains fewer variables than the original dataset. The price of that is creating and storing a bunch of local macros containing variable labels that are not needed subsequently. If desired, you can avoid this by using a more explicit list of variable names in place of the wildcard *, such as
. foreach v of var a b c d e { . local l`v' : variable label `v' . if `"`l`v''"' == "" { . local l`v' "`v'" . } . }
Variations on this situation are easy to manage. Suppose, for example, your convention was to name variables containing means with a prefix mean and those containing medians with a prefix med. Then you could use usubstr() to strip the prefix before referring to the saved macro,
. foreach v of var mean* { . local o = usubstr("`v'",5,.) . label var `v' "mean of `l`o''" . } . foreach v of var med* { . local o = usubstr("`v'",4,.) . label var `v' "median of `l`o''" . }
assuming that no other variables have names beginning with these prefixes.
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.