Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: could someone pls explain me this


From   Nick Cox <njcoxstata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: could someone pls explain me this
Date   Mon, 30 Apr 2012 19:33:12 +0100

You need to think of what the problem is to which this code is a solution.

What is the value of a string variable corresponding to a value of a
numeric variable? You can't say

summarize <strvar> if <numvar> == #

as -summarize- doesn't summarize string variables. So, you need to
find out _where_ in the data it is true that

<numvar>== #

i.e. use a look-up technique. If a variable contains observation
numbers, then r(min) and r(max) after -summarize- applied to that
variable will also contain observation numbers.

Nick

On Mon, Apr 30, 2012 at 7:24 PM, tashi lama <ltashi32@hotmail.com> wrote:
> thank you. I still have some clouds.
>
> 1.Why do we have to summarize the temp var?
>
> 2.Minimum such obs no r(min). Are we looking the minimum obs in the tempvar? Is it 3 in the following example?
>
>
>
> epcom                 noncom          __000001 |
>     |------------------------------------------|
>  1. |  18001                Acheres          . |
>  2. |  18002         Ainay-Le_Vieil          . |
>  3. |  18003   Les Aix -d'Angillion          3 |
>  4. |  18004                Allogny          . |
>  5. |  18005                Allouis          . |
>
>
>
>
> 3. Could we have said instead of minimum or maximum no, non-missing obs no because there is only one obs in tempvar?
>
> ----------------------------------------
>> Date: Mon, 30 Apr 2012 18:37:56 +0100
>> Subject: Re: st: could someone pls explain me this
>> From: njcoxstata@gmail.com
>> To: statalist@hsphsun2.harvard.edu
>>
>> 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 <ltashi32@hotmail.com> 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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index