Is there a command for picking off the lowest values of an Identified group?

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Is there a command for picking off the lowest values of an Identified group?

Ian Breunig
I want to find the age of the mother's youngest child.

If, for example, I have data as follows:

Mother ID:        Child ID:     Child Age:

1                         a                  1
1                         b                  2
1                         c                  3
1                         d                  4
2                         e                  10
2                         f                   10
2                         g                  12
3                         h                  5
3                         i                   7

Is there a simple command to tell me the youngest age for every Mother
ID or  do I have to use a do loop and if statement to do this?  If so
can anyone help me out with the most efficient way to code the do
loop.

I have used - collapse - and an if statement to count children under a
specified age before but I can't seem to find a command to do what I
want above.

Thanks ahead of time if you can help!

Ian
*
*   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/
Reply | Threaded
Open this post in threaded view
|

Re: Is there a command for picking off the lowest values of an Identified group?

nshephard
Administrator
On Thu, Jul 29, 2010 at 3:43 PM, Ian Breunig <[hidden email]> wrote:

> I want to find the age of the mother's youngest child.
>
> If, for example, I have data as follows:
>
> Mother ID:        Child ID:     Child Age:
>
> 1                         a                  1
> 1                         b                  2
> 1                         c                  3
> 1                         d                  4
> 2                         e                  10
> 2                         f                   10
> 2                         g                  12
> 3                         h                  5
> 3                         i                   7
>
> Is there a simple command to tell me the youngest age for every Mother
> ID or  do I have to use a do loop and if statement to do this?  If so
> can anyone help me out with the most efficient way to code the do
> loop.

No loop required, the following should do what you are after (note
I've assumed variable names, as the above are illegal in Stata as
spaces in variable names are not permitted)

bysort mother_id (child_age) : gen youngest_child = child_age[1]

The variable youngest_child will be constant within each grouping of
mother_id and will contain the age of the youngest child (by virtue of
having sorted the data on that variable).

Neil

--
"... no scientific worker has a fixed level of significance at which
from year to year, and in all circumstances, he rejects hypotheses; he
rather gives his mind to each particular case in the light of his
evidence and his ideas." - Sir Ronald A. Fisher (1956)

Email - [hidden email]
Website - http://slack.ser.man.ac.uk/
Photos - http://www.flickr.com/photos/slackline/

*
*   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/
Reply | Threaded
Open this post in threaded view
|

AW: Is there a command for picking off the lowest values of an Identified group?

Martin Weiss-5
In reply to this post by Ian Breunig

<>



*************
clear*

inp byte Mother_ID str1 Child_ID byte Child_Age
1   a  1
1   b  2
1   c  3
1   d  4
2   e  10
2   f   10
2   g  12
3   h  5
3   i   7
end

bys Mother_ID: egen minagechild=min(Child_Age)

li, noo sepby(Mother_ID)

*************

See NJC`s http://www.stata-journal.com/sjpdf.html?articlenum=pr0004


HTH
Martin


-----Ursprüngliche Nachricht-----
Von: [hidden email]
[mailto:[hidden email]] Im Auftrag von Ian Breunig
Gesendet: Donnerstag, 29. Juli 2010 17:43
An: [hidden email]
Betreff: st: Is there a command for picking off the lowest values of an
Identified group?

I want to find the age of the mother's youngest child.

If, for example, I have data as follows:

Mother ID:        Child ID:     Child Age:

1                         a                  1
1                         b                  2
1                         c                  3
1                         d                  4
2                         e                  10
2                         f                   10
2                         g                  12
3                         h                  5
3                         i                   7

Is there a simple command to tell me the youngest age for every Mother
ID or  do I have to use a do loop and if statement to do this?  If so
can anyone help me out with the most efficient way to code the do
loop.

I have used - collapse - and an if statement to count children under a
specified age before but I can't seem to find a command to do what I
want above.

Thanks ahead of time if you can help!

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


*
*   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/
Reply | Threaded
Open this post in threaded view
|

Re: Is there a command for picking off the lowest values of an Identified group?

Ian Breunig
In reply to this post by Ian Breunig
Whoops, even though it doesn't matter, I meant that I used - contract
- not -collapse -

Ian

On Thu, Jul 29, 2010 at 9:43 AM, Ian Breunig <[hidden email]> wrote:

> I want to find the age of the mother's youngest child.
>
> If, for example, I have data as follows:
>
> Mother ID:        Child ID:     Child Age:
>
> 1                         a                  1
> 1                         b                  2
> 1                         c                  3
> 1                         d                  4
> 2                         e                  10
> 2                         f                   10
> 2                         g                  12
> 3                         h                  5
> 3                         i                   7
>
> Is there a simple command to tell me the youngest age for every Mother
> ID or  do I have to use a do loop and if statement to do this?  If so
> can anyone help me out with the most efficient way to code the do
> loop.
>
> I have used - collapse - and an if statement to count children under a
> specified age before but I can't seem to find a command to do what I
> want above.
>
> Thanks ahead of time if you can help!
>
> Ian
> *
> *   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/
>

*
*   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/
Reply | Threaded
Open this post in threaded view
|

Re: Is there a command for picking off the lowest values of an Identified group?

Ian Breunig
In reply to this post by nshephard
Thanks a lot Neil!  You saved me some time this morning.

Ian

On Thu, Jul 29, 2010 at 9:49 AM, Neil Shephard <[hidden email]> wrote:

> On Thu, Jul 29, 2010 at 3:43 PM, Ian Breunig <[hidden email]> wrote:
>> I want to find the age of the mother's youngest child.
>>
>> If, for example, I have data as follows:
>>
>> Mother ID:        Child ID:     Child Age:
>>
>> 1                         a                  1
>> 1                         b                  2
>> 1                         c                  3
>> 1                         d                  4
>> 2                         e                  10
>> 2                         f                   10
>> 2                         g                  12
>> 3                         h                  5
>> 3                         i                   7
>>
>> Is there a simple command to tell me the youngest age for every Mother
>> ID or  do I have to use a do loop and if statement to do this?  If so
>> can anyone help me out with the most efficient way to code the do
>> loop.
>
> No loop required, the following should do what you are after (note
> I've assumed variable names, as the above are illegal in Stata as
> spaces in variable names are not permitted)
>
> bysort mother_id (child_age) : gen youngest_child = child_age[1]
>
> The variable youngest_child will be constant within each grouping of
> mother_id and will contain the age of the youngest child (by virtue of
> having sorted the data on that variable).
>
> Neil
>
> --
> "... no scientific worker has a fixed level of significance at which
> from year to year, and in all circumstances, he rejects hypotheses; he
> rather gives his mind to each particular case in the light of his
> evidence and his ideas." - Sir Ronald A. Fisher (1956)
>
> Email - [hidden email]
> Website - http://slack.ser.man.ac.uk/
> Photos - http://www.flickr.com/photos/slackline/
>
> *
> *   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/
>

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