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: Changing labels for multiple variables at the same time
From
Anna Reimondos <[email protected]>
To
[email protected]
Subject
Re: st: Changing labels for multiple variables at the same time
Date
Sun, 11 Apr 2010 12:38:47 +1000
Dear Michael,
Thank you so much - it works perfectly for what I want to do!
I did not realize you could do that with the dir command, but I think
it is definitely something I might use a lot in the future.
What an excellent and elegant solution.
I am very grateful,
Anna
On Sun, Apr 11, 2010 at 12:02 PM, Michael Norman Mitchell
<[email protected]> wrote:
> Dear Anna
>
> This is an awesome question... I think it really shows off one of the
> strengths of Stata because this is so easy in Stata. The key is the -label
> dir- command that gives a list of all of the value labels in memory, and
> returns that list as -r(names)- . Then, a -foreach- loop is used to loop
> across all of those names of the labels to modify them as you see fit. Here
> is an example...
>
> * use a dataset
> sysuse nlsw88
>
> * show existing labels
> label list
>
> * get list of existing labels, stored in r(names)
> label dir
>
> * go through all labels, and modify them
> foreach lab in `r(names)' {
> label define `lab' -1 "Missing", modify
> label define `lab' -3 "Not applicable", modify
> }
>
> * show the modified labels
> label list
>
>
> and here is the output this generates. Note how all of the labels,
> afterward, have a value labeled for -1 and -3.
>
> . * use a dataset
> . sysuse nlsw88
> (NLSW, 1988 extract)
>
> .
> . * show existing labels
> . label list
> occlbl:
> 1 Professional/technical
> 2 Managers/admin
> 3 Sales
> 4 Clerical/unskilled
> 5 Craftsmen
> 6 Operatives
> 7 Transport
> 8 Laborers
> 9 Farmers
> 10 Farm laborers
> 11 Service
> 12 Household workers
> 13 Other
> indlbl:
> 1 Ag/Forestry/Fisheries
> 2 Mining
> 3 Construction
> 4 Manufacturing
> 5 Transport/Comm/Utility
> 6 Wholesale/Retail Trade
> 7 Finance/Ins/Real Estate
> 8 Business/Repair Svc
> 9 Personal Services
> 10 Entertainment/Rec Svc
> 11 Professional Services
> 12 Public Administration
> racelbl:
> 1 white
> 2 black
> 3 other
> marlbl:
> 0 single
> 1 married
> gradlbl:
> 0 not college grad
> 1 college grad
> smsalbl:
> 0 nonSMSA
> 1 SMSA
> unionlbl:
> 0 nonunion
> 1 union
>
> .
> . * get list of existing labels, stored in r(names)
> . label dir
> occlbl
> indlbl
> racelbl
> marlbl
> gradlbl
> smsalbl
> unionlbl
>
> .
> . * go through all labels, and modify them
> . foreach lab in `r(names)' {
> 2. label define `lab' -1 "Missing", modify
> 3. label define `lab' -3 "Not applicable", modify
> 4. }
>
> .
> . * show the modified labels
> . label list
> unionlbl:
> -3 Not applicable
> -1 Missing
> 0 nonunion
> 1 union
> smsalbl:
> -3 Not applicable
> -1 Missing
> 0 nonSMSA
> 1 SMSA
> gradlbl:
> -3 Not applicable
> -1 Missing
> 0 not college grad
> 1 college grad
> marlbl:
> -3 Not applicable
> -1 Missing
> 0 single
> 1 married
> racelbl:
> -3 Not applicable
> -1 Missing
> 1 white
> 2 black
> 3 other
> indlbl:
> -3 Not applicable
> -1 Missing
> 1 Ag/Forestry/Fisheries
> 2 Mining
> 3 Construction
> 4 Manufacturing
> 5 Transport/Comm/Utility
> 6 Wholesale/Retail Trade
> 7 Finance/Ins/Real Estate
> 8 Business/Repair Svc
> 9 Personal Services
> 10 Entertainment/Rec Svc
> 11 Professional Services
> 12 Public Administration
> occlbl:
> -3 Not applicable
> -1 Missing
> 1 Professional/technical
> 2 Managers/admin
> 3 Sales
> 4 Clerical/unskilled
> 5 Craftsmen
> 6 Operatives
> 7 Transport
> 8 Laborers
> 9 Farmers
> 10 Farm laborers
> 11 Service
> 12 Household workers
> 13 Other
>
> .
> end of do-file
>
> Hope that does the trick.
>
> Best regards,
>
> Michael N. Mitchell
> See the Stata tidbit of the week at...
> http://www.MichaelNormanMitchell.com
>
> On 2010-04-10 6.22 PM, Anna Reimondos wrote:
>>
>> Hello,
>> I would like to define a some new value labels which I want to attach
>> to all existing value labels in my dataset.
>>
>> For example, I want to define among other things that -1 is Missing,
>> and -3 is Not applicable:
>>
>> sysuse nlsw88, clear
>> label define gradlbl -1 "Missing", modify
>> label define gradlbl -3 "Not applicable", modify
>>
>> However I want to modify all the labels (a couple of hundred labels in
>> the dataset I am working on) in the same way not just gradlbl.
>>
>> Does anyone know of a way to do this? I tried using the extended macro
>> functions but without much luck.
>>
>> Thanks very much,
>> Anna
>> *
>> * 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/
>
*
* 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/