Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: Re: if command


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: Re: if command
Date   Fri, 13 Feb 2004 11:11:31 -0000

Kit is on the target on the syntax issue, but 
I'm not sure how far this is the right 
direction for solving Danielle's 
underlying problem. Usually in similar 
situations I would define "Birth rate" etc. 
as variable labels and then just pick 
up the variable label. That would avoid 
a complicated branching structure and the 
whole could boil down to something like 

local title `"`:var label `1''"' 

which in practice I would normally 
extend to 

local title `"`:var label `1''"' 
if `"`title'"' == "" local title "`1'" 

Nick 
[email protected] 

Christopher F Baum

> On Feb 13, 2004, at 2:33 AM, Danielle wrote:
> 
> > Hmm, actually, perhaps I should have been more clear on 
> what I am doing
> > here. I am not actually using the values of the variable <aRate> in
> > these statements. The basic idea is I want to run 
> essentially the same
> > procedure several times on different variables. There are style-type
> > things I want to change depending on what that variable is 
> (i.e. title
> > on graph), so I write conditions to differentiate between 
> the relevant
> > variable. So, a snipped version of my program would look like this:
> >
> > program test
> >
> > ...
> >
> > if `1'==aRate {
> > 	local title = "Abortion rate"
> > }
> >
> > if `1'==bRate {
> > 	local title = "Birth rate"
> > }
> 
> This is not actually an issue of how -if- works; it relates to how 
> Stata passes arguments to programs. The first argument to the program 
> is passed in local macro 1. If it is a string -- such as a variable 
> name -- then you can use the contents of local macro 1, that is, `1', 
> in a command where you want that variable name (such as 'summarize' 
> below). But if you want to examine the variable name and 
> compare it to 
> a string, you must treat the contents of local macro 1 as a string, 
> that is, "`1'", which can be compared to a string "aRate". If 
> the item 
> on the right side above is not quoted, it is not the string aRate; it 
> is a reference to the first observation of the variable 
> aRate. If both 
> aRate and bRate happened to have the same first observation, 
> the result 
> of your program would be unpredictable. A reliable form of this code 
> is:
> 
> program test
> if "`1'" == "aRate" {
> 	local title = "Abortion rate"
> }
> 
> if "`1'" == "bRate" {
> 	local title = "Birth rate"
> }
> di "`title'"
> summarize `1'
> end

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index