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

st: Re: if command


From   Christopher F Baum <[email protected]>
To   [email protected]
Subject   st: Re: if command
Date   Fri, 13 Feb 2004 05:12:36 -0500

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

Kit

*
* 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