loop question

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

loop question

P C-3
Hi,

I have a loop question.

I want to do something like this:

local i=0
gen w1ttflsbyger=.
foreach n in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20{
 replace w1ttflsbyger=`i'+1 if w1flsibage`n'<w1ageint & w1flsibage`n'!=. &
w1ageint~=.
 replace i=`i'+1 if w1flsibage`n'<w1ageint & w1flsibage`n'!=. & w1ageint~=.
}

Of course, the last line of the syntax was wrong. Basically, I want to write
something to let "i" to increase by 1 if the condition: "w1flsibage`n'<w1ageint
& w1flsibage`n'!=. & w1ageint~=." are satisfied. Otherwise, "i" won't increase
by 1 and will jump out of the loop.

How should I write the loop?

Thanks!


     

*
*   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: loop question

Nick Cox
First off, a -foreach- loop over the integers 1/20 is better written as a -forval- loop

forval n = 1/20 {

}

But more importantly,

1. Looping sounds a bad way to do what you want.  

2. You are treating macros as if they were variables, which is a major misunderstanding.

You'd be better off explaining the structure of your data and what you want to do in words, and then you'll probably get better advice on what you should do.

Nick
[hidden email]

P.S. please see Statalist advice:

http://www.stata.com/support/faqs/res/statalist.html#tojoin

especially 2.1.3.

P C
===

I have a loop question.

I want to do something like this:

local i=0
gen w1ttflsbyger=.
foreach n in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20{
 replace w1ttflsbyger=`i'+1 if w1flsibage`n'<w1ageint & w1flsibage`n'!=. &
w1ageint~=.
 replace i=`i'+1 if w1flsibage`n'<w1ageint & w1flsibage`n'!=. & w1ageint~=.
}

Of course, the last line of the syntax was wrong. Basically, I want to write
something to let "i" to increase by 1 if the condition: "w1flsibage`n'<w1ageint
& w1flsibage`n'!=. & w1ageint~=." are satisfied. Otherwise, "i" won't increase
by 1 and will jump out of the loop.

How should I write the loop?


*
*   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: loop question

Sergiy Radyakin
In reply to this post by P C-3
On Tue, Nov 30, 2010 at 11:37 AM, P C <[hidden email]> wrote:

> Hi,
>
> I have a loop question.
>
> I want to do something like this:
>
> local i=0
> gen w1ttflsbyger=.
> foreach n in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20{
>  replace w1ttflsbyger=`i'+1 if w1flsibage`n'<w1ageint & w1flsibage`n'!=. &
> w1ageint~=.
>  replace i=`i'+1 if w1flsibage`n'<w1ageint & w1flsibage`n'!=. & w1ageint~=.
> }
>
> Of course, the last line of the syntax was wrong. Basically, I want to write
> something to let "i" to increase by 1 if the condition: "w1flsibage`n'<w1ageint
> & w1flsibage`n'!=. & w1ageint~=." are satisfied.

if (w1flsibage`n'<w1ageint & w1flsibage`n'!=. & w1ageint~=.) local i=`i'+1

Also use -forval- instead of -foreach- (if such loop is at all
necessary), if you
miss 13 in the above list, you will have a hard time finding it.

Also you would typically want to avoid this:
w1ageint~=.

Write intstead: !missing(w1ageint)
It is easier to read, and makes sure that you treat extended missings
same as the
system missing. How do you treat them in your current code?

Best, S.R.

> Otherwise, "i" won't increase
> by 1 and will jump out of the loop.
>
> How should I write the loop?
>
> Thanks!
>
>
>
>
> *
> *   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: loop question

P C-3
In reply to this post by Nick Cox
Thanks for the reply.

Basically, I have twenty variables in the data set. They have the same suffix:
w1flsibage. They all ended up with different numbers. The twenty variables
go like this: w1flsibage1 w1flsibage2 .... w1flsibage20. These variables
indicate sibling's age.


Then I compare each of the above variable with another variable: w1ageint
(respondent's age).


I need to calculate how many of the above 20 variables have values that are
greater than w1ageint. In this way, I would know how many siblings a respondent
would have that are older the respondent.

In this case, how should I write out the program?

Thanks



----- Original Message ----
From: Nick Cox <[hidden email]>
To: "[hidden email]" <[hidden email]>
Sent: Tue, November 30, 2010 11:55:27 AM
Subject: st: RE: loop question

First off, a -foreach- loop over the integers 1/20 is better written as a
-forval- loop


forval n = 1/20 {

}

But more importantly,

1. Looping sounds a bad way to do what you want. 

2. You are treating macros as if they were variables, which is a major
misunderstanding.

You'd be better off explaining the structure of your data and what you want to
do in words, and then you'll probably get better advice on what you should do.


Nick
[hidden email]

P.S. please see Statalist advice:

http://www.stata.com/support/faqs/res/statalist.html#tojoin

especially 2.1.3.

P C
===

I have a loop question.

I want to do something like this:

local i=0
gen w1ttflsbyger=.
foreach n in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20{
 replace w1ttflsbyger=`i'+1 if w1flsibage`n'<w1ageint & w1flsibage`n'!=. &
w1ageint~=.
 replace i=`i'+1 if w1flsibage`n'<w1ageint & w1flsibage`n'!=. & w1ageint~=.
}

Of course, the last line of the syntax was wrong. Basically, I want to write
something to let "i" to increase by 1 if the condition: "w1flsibage`n'<w1ageint
& w1flsibage`n'!=. & w1ageint~=." are satisfied. Otherwise, "i" won't increase
by 1 and will jump out of the loop.

How should I write the loop?


*
*  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: loop question

Nick Cox
local stuff w1flsibage
gen count = 0
qui forval j = 1/20 {
        replace count = count + (`stuff'`j' > w1ageint & !missing(`stuff'`j'))
}

Here `stuff' is just a substitution to make the principle clearer.

(`stuff'`j' > w1ageint & !missing(`stuff'`j'))

will be 1 whenever both conditions are satisfied and 0 otherwise.

See e.g.

FAQ     . . . . . . . . . . . . . . . . . . . . . . .  True and false in Stata
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
        2/03    What is true and false in Stata?
                http://www.stata.com/support/faqs/data/trueorfalse.html

if more detail is needed. Missings need care, as your original post showed.

Nick
[hidden email]

P C

Thanks for the reply.

Basically, I have twenty variables in the data set. They have the same suffix:
w1flsibage. They all ended up with different numbers. The twenty variables
go like this: w1flsibage1 w1flsibage2 .... w1flsibage20. These variables
indicate sibling's age.


Then I compare each of the above variable with another variable: w1ageint
(respondent's age).


I need to calculate how many of the above 20 variables have values that are
greater than w1ageint. In this way, I would know how many siblings a respondent
would have that are older the respondent.

In this case, how should I write out the program?

Nick Cox <[hidden email]>

First off, a -foreach- loop over the integers 1/20 is better written as a
-forval- loop


forval n = 1/20 {

}

But more importantly,

1. Looping sounds a bad way to do what you want. 

2. You are treating macros as if they were variables, which is a major
misunderstanding.

You'd be better off explaining the structure of your data and what you want to
do in words, and then you'll probably get better advice on what you should do.

P.S. please see Statalist advice:

http://www.stata.com/support/faqs/res/statalist.html#tojoin

especially 2.1.3.

P C
===

I have a loop question.

I want to do something like this:

local i=0
gen w1ttflsbyger=.
foreach n in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20{
 replace w1ttflsbyger=`i'+1 if w1flsibage`n'<w1ageint & w1flsibage`n'!=. &
w1ageint~=.
 replace i=`i'+1 if w1flsibage`n'<w1ageint & w1flsibage`n'!=. & w1ageint~=.
}

Of course, the last line of the syntax was wrong. Basically, I want to write
something to let "i" to increase by 1 if the condition: "w1flsibage`n'<w1ageint
& w1flsibage`n'!=. & w1ageint~=." are satisfied. Otherwise, "i" won't increase
by 1 and will jump out of the loop.

How should I write the loop?

*
*   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: loop question

P C-3
Thanks a lot!



----- Original Message ----
From: Nick Cox <[hidden email]>
To: "[hidden email]" <[hidden email]>
Sent: Tue, November 30, 2010 2:37:38 PM
Subject: RE: st: RE: loop question

local stuff w1flsibage
gen count = 0
qui forval j = 1/20 {
    replace count = count + (`stuff'`j' > w1ageint & !missing(`stuff'`j'))
}

Here `stuff' is just a substitution to make the principle clearer.

(`stuff'`j' > w1ageint & !missing(`stuff'`j'))

will be 1 whenever both conditions are satisfied and 0 otherwise.

See e.g.

FAQ    . . . . . . . . . . . . . . . . . . . . . . .  True and false in Stata
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
        2/03    What is true and false in Stata?
                http://www.stata.com/support/faqs/data/trueorfalse.html

if more detail is needed. Missings need care, as your original post showed.

Nick
[hidden email]

P C

Thanks for the reply.

Basically, I have twenty variables in the data set. They have the same suffix:
w1flsibage. They all ended up with different numbers. The twenty variables
go like this: w1flsibage1 w1flsibage2 .... w1flsibage20. These variables
indicate sibling's age.


Then I compare each of the above variable with another variable: w1ageint
(respondent's age).


I need to calculate how many of the above 20 variables have values that are
greater than w1ageint. In this way, I would know how many siblings a respondent
would have that are older the respondent.

In this case, how should I write out the program?

Nick Cox <[hidden email]>

First off, a -foreach- loop over the integers 1/20 is better written as a
-forval- loop


forval n = 1/20 {

}

But more importantly,

1. Looping sounds a bad way to do what you want. 

2. You are treating macros as if they were variables, which is a major
misunderstanding.

You'd be better off explaining the structure of your data and what you want to
do in words, and then you'll probably get better advice on what you should do.

P.S. please see Statalist advice:

http://www.stata.com/support/faqs/res/statalist.html#tojoin

especially 2.1.3.

P C
===

I have a loop question.

I want to do something like this:

local i=0
gen w1ttflsbyger=.
foreach n in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20{
 replace w1ttflsbyger=`i'+1 if w1flsibage`n'<w1ageint & w1flsibage`n'!=. &
w1ageint~=.
 replace i=`i'+1 if w1flsibage`n'<w1ageint & w1flsibage`n'!=. & w1ageint~=.
}

Of course, the last line of the syntax was wrong. Basically, I want to write
something to let "i" to increase by 1 if the condition: "w1flsibage`n'<w1ageint
& w1flsibage`n'!=. & w1ageint~=." are satisfied. Otherwise, "i" won't increase
by 1 and will jump out of the loop.

How should I write the loop?

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