Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Question using macro in loop


From   Tak-wai Chau <[email protected]>
To   [email protected]
Subject   Re: st: Question using macro in loop
Date   Fri, 06 Apr 2007 13:09:35 -0400

Hi, Friedrich and other statalist subscribers,

Thanks for your reply, Friedrich!

The example in last paragraph concerning a particular observation.

In my example, the rationale here is to condense the list of variables to a shorter list of variables only including the non-missing values.

The key question I have in mind is whether we can, within a loop, update one of the variables, and which variable to update is according to the value taking by another variable. This value is different across observations. The example I have given is just one of its usage I want to do.

Another usage is to organize start and end time of an event while I am given the monthly status of the event. A variable is used to store which spell one is going through at that time, and the loop is going through the time.

Hope I am clearer this time.

Thanks a lot!

Tak Wai

Friedrich Huebler wrote:

Tak Wai,

It is not clear what you mean when you speak of observations and
variables. Does each v* variable have the same value in all
observations? When you say "v5 is missing", does that mean that it
has missing values in all observations? How exactly does your data
look like? What is the logic behind what you are trying to
accomplish?

If you want to count the number of missing values across all v*
variables for each observation, you can use -egen-.

. egen s = rowmiss(v1-v10)

Friedrich

--- Tak-wai Chau <[email protected]> wrote:
Hi, Friedrich and other statalisters,

Thanks for Friedrich's reply.

Sorry that I made a mistake in my code. I mean "gen s=1" instead of

"s=1". The missing values appear at different variables for
different observations, so s would take different values for different observations. Also, what I want is that, for example, if all Vi are
not missing except V5, then the resulting Wi would be that W1-W4 would
be the same as V1-V4, while W5-W9 would carry values from V6-V10.

Thanks a lot!

Tak Wai

Friedrich Huebler wrote:

Tak Wai,

The loop below copies all non-missing observations from variables
v1
to v10 to variables w1 to w10. The macro s counts the number of
non-missing observations in variables v1 to v10.

local s = 0
forvalues i = 1/10 {
  replace w`i' = v`i' if v`i'<.
  count if v`i'<.
  local s = `s' + r(N)
  di `s'
}

The statement "s=1" is not valid Stata, you probably mean
"generate
s=1". The command "local s1=s" assigns the first observation in
variable s to the local s1. The command "replace s=s+1 if v`i'<."
adds 1 to all observations in variable s if the first observation
in
variable v`i' is missing.

The command "replace w`s1'=v`i' if v`i'<." indicates that you may
be
thinking in terms of a loop over observations, not variables.
Such a
loop is neither efficient nor necessary but if you want it you
need
different code from the one above.

Friedrich

--- Tak-wai Chau <[email protected]> wrote:
Hi,

I would like to ask something about using macro in loops. I have
tried to find it in the archive and FAQ for some time but I
don't
get it.

Sorry if I have overlooked some obvious things.

I would like to update certain variables according to the value
taking by a particular variable, and this value differs by
observations. Then what I should do?

For example, if I want to transfer value of non-missing
variables
from V1-V10 to W1-W10. But I don't want to variables carrying missing value between. I use s to count the number of
non-missing
values I have covered at that point.

The following is what I think of but doesn't work.

s=1
forvalues i=1/10 {
	local s1=s
	replace w`s1'=v`i' if v`i'<.
	replace s=s+1 if v`i'<.
}

I know the problem is at the local statement.

What should I do?

Thank you very much!

Tak Wai Chau







____________________________________________________________________________________
No need to miss a message. Get email on-the-go with Yahoo! Mail for Mobile. Get started.
http://mobile.yahoo.com/mail *
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index