Statalist


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

RE: st: problem creating and doing a foreach on a local of allnumeric variables in a dataset


From   "Jennifer S. Earl" <[email protected]>
To   [email protected]
Subject   RE: st: problem creating and doing a foreach on a local of allnumeric variables in a dataset
Date   Wed, 27 Aug 2008 11:01:31 -0700

Thanks again Maarten for the additional working code and Nick for the excellent explanation. I really learned a lot.

--On Wednesday, August 27, 2008 5:20 PM +0100 Nick Cox <[email protected]> wrote:


Not that I have anything against -ds-, dear and sweet that it is, but
just to note that there are other ways to do this:

foreach var of var * {
	capture confirm numeric variable `var'
	if _rc {
		replace `var' = "" if `var'_error == 1
	}
	else {
		replace `var' = . if `var'_error == 1
	}
}

Occasionally I have wanted a function say -sysmiss(<arg>)- that returns
string missing "" if <arg> is string and . otherwise. I am not sure if
it's really a good idea!

Nick
[email protected]

Nick Cox

To expand a little:

local num_vars r(varlist)

will just put the literal text "r(varlist)" in your macro. It won't put
the contents of r(varlist) in your macro.

Also

local num_vars r(varlist)
foreach var of local `num_vars' {

is equivalent to

foreach var of local r(varlist) {

which doesn't satisfy -foreach- because r(varlist) is not the name of a
local macro.

It is true that the error messages in each case are not illuminating!

Nick
[email protected]

Maarten buis

try:
foreach var of varlist `r(varlist)' {

That is: adding a "`" and a "'" around r(varlist)

Jennifer S. Earl

In the middle of a program I am working on, I need to set a bunch of
variables to missing, some of which are numeric and some of which are

string. Since the replace command is sensitive to numeric or string,
I need
to do separate foreach loops.

Here is what I have tried:

ds , has(type numeric)
local num_vars r(varlist)
foreach var of local num_vars {
	replace `var'=. if `var'_error==1
}

ds , has(type string)
local string_vars r(varlist)
foreach var of local string_vars {
	replace `var'="" if `var'_error==1
}

Unfortunately, when I try this, I get the following error:
time-series operators not allowed
r(101);

If I do instead (in case if is my foreach line that is giving me
problems):
ds , has(type numeric)
local num_vars r(varlist)
foreach var of local `num_vars' {
	replace `var'=. if `var'_error==1
}

I get the following error:
{ required
r(100);

But, the puzzling part is that I see the right list of numeric
variables
displayed if I do:

ds , has(type numeric)
local num_vars r(varlist)
display `num_vars'

So, I know I am fetching the right list, I am just not able to
operate on
it in the foreach loop.

I have tried other ways of selecting off just the numeric variables
and
just the string and none are working.

Any suggestions? Maybe it is late and I missing something obvious?
*
*   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/


*********************************
Jennifer Earl
Director, Center for Information Technology and Society
Associate Professor of Sociology
University of California, Santa Barbara
Santa Barbara, CA  93106-9430

P: (805) 893-7471
F: (805) 893-3324
*********************************
*
*   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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index