How to display or extract only the label of a variable?

How to display or extract only the label of a variable?

by Tiffany Ho -
Number of replies: 6

Hi,

I've made a for loop to create some figures and I want to use the *label* of the response variable to help name the figure and use it to describe the axis rather than the variable name. I've tried Googling for this information but haven't found a solution. I could just manually change the axis name but thought this would make life easier and the code more elegant.

 

Thanks.

In reply to Tiffany Ho

Re: How to display or extract only the label of a variable?

by Wayne Enanoria -

Can you post a snippet of your code so I can see how you set up the loop and what it does? I can then propose or suggest a solution.

In reply to Wayne Enanoria

Re: How to display or extract only the label of a variable?

by Tiffany Ho -

sure, this is my loop:

 

foreach yvar in lefthipp_tbv righthipp_tbv leftamyg_tbv rightamyg_tbv {
graph box `yvar' ///
, over(female, relabel (1 "female" 2 "male") ///
label(labsize(large))) ///
over(groupnum, relabel (1 "AT" 2 "MDD" 3 "NCL") ///
label(labsize(vlarge))) ///
title("Figure XX", size(vlarge)) ///
ytitle("`yvar'", size(large) margin(small)) ///
graphregion(fcolor(white) lcolor(none)) ///
scheme(s2mono) ///
name(`yvar'_box, replace) ///
saving("`yvar'.graph", replace)
}

as you can see it would be nice to use the label of the variable rather than the variable name, both when creating the figure and saving a file. I also wrote "XX" for the Figure title too since I'm not sure how one can keep track of the loop # with this arrangement. I suppose you can declare a variable that starts at 1 at the beginning of the loop and increases by 1 at the end of the loop, but I just realized I don't even know how to do that! Generating a variable will create one that has the length of the # of observations I have so it seems easier if I just manually change things myself.

Any insight on this would be appreciated - thanks

In reply to Tiffany Ho

Re: How to display or extract only the label of a variable?

by Wayne Enanoria -

Would something like this work? See http://www.stata.com/statalist/archive/2006-10/msg00909.html . It is a simplified version of your code but worth a try.

In reply to Wayne Enanoria

Re: How to display or extract only the label of a variable?

by Tiffany Ho -

yes it works! very nice!

 

do you happen to have any ideas on how to keep track of loop # (see my previous message). thanks!

In reply to Tiffany Ho

Re: How to display or extract only the label of a variable?

by Megha Mehrotra -

Usually I generate an indicator within the for loop and add 1 to it after each iteration. So it'd look something like:

foreach var in ....... {

gen k=0

....

other commands....

...

replace k=k+1

}

Let me know if that doesn't work for your purposes and I can try and see if I can come up with something else.

Megha

In reply to Megha Mehrotra

Re: How to display or extract only the label of a variable?

by Tiffany Ho -

yes but how do you then display the value of k within the title command when editing a Figure? it doesn't work when i use `k' or any other quotation combo or display k.