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: could someone pls explain me this


From   Nick Cox <[email protected]>
To   [email protected]
Subject   Re: st: could someone pls explain me this
Date   Mon, 30 Apr 2012 18:37:56 +0100

The code

 tempvar tv1
 gene `tv1'=_n if depcom==18003
 summ `tv1', meanonly
 local index=r(min)
 local mymacro=nomcom[`index']
 drop `tv1'

is a way of looking up the value of -nomcom- when -depcom- is 18003.
The temporary variable contains observation numbers for which -depcom-
is 18003. A certain way of finding such an observation if any exist is
to find the minimum such observation number. The maximum would do fine
as an alternative. The code could be shortened

tempvar tv1
gene `tv1'=_n if depcom==18003
summ `tv1', meanonly
local mymacro=nomcom[r(min)]
drop `tv1'

The main point of similar code is to automate value look-up. It's
usually a long-winded way to proceed if you are working interactively.

Nick

On Mon, Apr 30, 2012 at 6:24 PM, tashi lama <[email protected]> wrote:

> I am doing sth similar to what I found in archive titiled "Re: st: save the value of a string variable into a macro". I am not sure if I followed the code although it looks relatively st. forward.
>
>
>
> Here is the dataset and goal
>
>  +-----------------------------+
> | depcom nomcom |
> |-----------------------------|
> 1. | 18001 Acheres |
> 2. | 18002 Ainay-le-Vieil |
> 3. | 18003 Les Aix-d'Angillon |
> 4. | 18004 Allogny |
> 5. | 18005 Allouis |
> +-----------------------------+
>
> Where depcom is an id. I want to save a specific values of the variable "nomcom" into a variable, for example, I want to obtain :
> . di "`mymacro'"
> Archeres
>
>
>
> The code is
>
>
>
> tempvar tv1
> gene `tv1'=_n if depcom==18003
> summ `tv1', meanonly
> local index=r(min)
> local mymacro=nomcom[`index']
> drop `tv1'
>
>
>
>
> I follow line 1 and 2. I am not sure why we need line 3. Line 4 established a macro which is used in line 5. But, I don't follow why the macro takes r(min) as expression. I would think the macro `index' should capture the value(the only value in fact) in tv1.

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