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: Looping Variable Labels Define


From   Maarten Buis <[email protected]>
To   [email protected]
Subject   Re: st: Looping Variable Labels Define
Date   Tue, 19 Feb 2013 09:30:45 +0100

On Tue, Feb 19, 2013 at 8:47 AM, Beatrice Benavidez wrote:
> I generated value labels for variables code_outlet and code_topic -
> code_outlet: 1 "Restaurant"     2 "Lounge" 3 "Bar" 4 "Minibar" 5 "Room Service"
>
> code_topic: 1 "Breakfast" 2 "Lunch" 3 "Afternoon Tea" 4 "Dinner" 5
> "Night" 6 "Minibar"
>
> I loop the creation of code_outlet_topic which is an amalgamation of
> code_outlet and code_topic-
>
> I would like to then have labels for code_outlet_topic such as:
>
> label define code_outlet topic 110 "Restaurant - Breakfast" 120
> "Restaurant - Lunch" ... 550 "Room Service - Night
>
> Would there be a way to do label the variable without having to do
> them one by one as it is composed purely of other variable labels and
> a dash?

You can use extended macro functions for that. These are documented in
-help extended_fcn-. I usually forget that name for the help-file, so
I type -help macro- and click on the link to the help file for
extended macro functions. Below is an example:

*------------------ begin example ------------------
// create an example dataset with all combinations
// of code_outlet and code_topic
clear all
set obs 5
gen byte code_outlet = _n
expand 6
bys code_outlet : gen byte code_topic = _n
gen int code_outlet_topic = 100*code_outlet + 10*code_topic

label define code_outlet 1 "Restaurant" ///
                         2 "Lounge"     ///
                         3 "Bar"        ///
                         4 "Minibar"    ///
                         5 "Room Service"
label value code_outlet code_outlet

label define code_topic 1 "Breakfast"     ///
                        2 "Lunch"         ///
                        3 "Afternoon Tea" ///
                        4 "Dinner"        ///
                        5 "Night"         ///
                        6 "Minibar"						
label value code_topic code_topic

// see the example dataset						
list, sepby(code_outlet)

// create the labels
forvalues out = 1/5 {
	forvalues top = 1/6 {
		local value = 100*`out'+10*`top'
		local lab_o : label (code_outlet) `out'
		local lab_t : label (code_topic) `top'
		local lab `"`lab_o' - `lab_t'"'
		label define code_outlet_topic `value' `"`lab'"', add
	}
}
label value code_outlet_topic code_outlet_topic

// see the result
list, sepby(code_outlet)
*------------------- end example -------------------
(For more on examples I sent to the Statalist see:
http://www.maartenbuis.nl/example_faq )

Hope this helps,
Maarten

---------------------------------
Maarten L. Buis
WZB
Reichpietschufer 50
10785 Berlin
Germany

http://www.maartenbuis.nl
---------------------------------
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index