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

st: RE: Re: More on passing one value inside a variable to a local macro


From   "Naheed Mirza" <[email protected]>
To   <[email protected]>
Subject   st: RE: Re: More on passing one value inside a variable to a local macro
Date   Sun, 18 Apr 2004 11:04:40 +0200

To add to this: what if the variable var2 is string?

-summarize- will not help in this case.

One possibility is

tempvar order
gen long `order' = _n
su `order' if var2 == "value", meanonly
local macroname = var1[`r(min)']

That is, the observation number you need is returned in r(min),
and then used to identify the value of -var1-.

Another possibility is

tempvar true
gen byte `true' = var2 == "`value'"
sort `true'
local macroname = var1[_N]

-----Original Message-----
From: Naheed Mirza [mailto:[email protected]]
Sent: 18. april 2004 00:36
To: '[email protected]'
Subject: Re: More on passing one value inside a variable to a local
macro


Renzo Comolli

I don't believe I am confusing the two -if- (although maybe I am, but I
don't think so).

I want the local macro to take the value of var1 for the same row for which
var2 takes a specific value, say 5 or "Texas". I know a priori that var2
will take that value only once, so macroname will end up containing only one
number coming from var1.
Example, my need is to find a way to do something like passing the GDP of
Texas to a local macro. Inside var1 I have all GDPs for each state and
inside var2 I have the name of all states. Something like this
. local macroname = var1 if var2=="Texas"
where I know a priori that Texas will appear only once in var2.
If I knew a priori that Texas where in the, say, 22nd observation, then I
would just write
. local macroname = var1[22]
and be done with it. But in the ado I am programming "Texas" can appear at
any place. Sorting would not help.

While writing for you what I meant with my work-around I understood why it
does not work. I am not sure that explaining my mistake would help. I think
it would just generate more confusion, so I omit it.

>>> One way of doing this

su var1 if var2 == <value>
local macroname "`r(min)'"

If, as you say, the value is unique, then -summarize- will
return the same value as, say, r(min) or r(max).

An assumption here is that -var1- is not string.


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