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: Assigning value of a function to a local macro


From   Nick Cox <[email protected]>
To   "'[email protected]'" <[email protected]>
Subject   RE: st: Assigning value of a function to a local macro
Date   Thu, 10 May 2012 17:15:28 +0100

That's the -egen- function -min()-. 

It's a completely different beast and nothing to do with what you were trying. 

I know that's confusing, but it's true. 

Any way, it won't work with -generate-, which was what you stated in your posting. Further, in any Stata context other than a call to -egen-, -min()- always means the -min()- function. Conversely, you can't use -egen- to produce a local or global macro or a scalar directly. 

To get the minimum as a single number in Stata, Phil's advice is good: use -summarize, meanonly-. 

Nick 
[email protected] 

Navid Asgari

E.g. we have this data:

     | var1 |
     |------|
  1. | 1995 |
  2. | 1994 |
  3. | 1991 |
  4. | 2001 |
  5. | 2003 |
     |------|
  6. | 2004

If you try to assign the minimum of "var1" to a local macro by using the following command you will get a syntax error:


local minvar=min(var1)
invalid syntax
r(198);

However, if you first create a variable containing the minimum of the "var1", then you can transfer that variable into your local macro:

. egen minivar1=min(var1)

. local minvar=minivar1


. display "the minimum of the varriable 1 is"   `minvar'

the minimum of the varriable 1 is1991


Nick Cox

Phil's solution doesn't involve scalars at all. 

I don't think your side-note is correct. At least, show us an example with real data. 

Nick 
[email protected] 

Navid Asgari

Thanks Phil! It is a good solution to use scalars.

But, on a side note, min() can work on one variable too

e.g.:

generate minv=min(variable1)

Phil Clayton

min() doesn't do what you think it does. It's more for this kind of thing:
gen newvar=min(var1, var2, var3)
or
gen newvar=min(var1, var1[_n-1])
or
summarize price
display min(r(mean), r(p50))

To get the lowest value of the variable year in the dataset, I would suggest:
summarize year, meanonly
local byear=r(min)

On 10/05/2012, at 7:40 PM, Navid Asgari wrote:
 
> I am trying to assign the value of a function to a local macro, but I get "Invalid Syntax"?
> 
> 
> I type:
> 
> local byear=min(year)
> 
> year is the variable whose minimum has to be saved in the local macro "byear"

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


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