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 at the end of May, and its replacement, statalist.org is already up and running.


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

RE: st: using arrays to make multiple indicator variables


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE: st: using arrays to make multiple indicator variables
Date   Sun, 14 Mar 2010 18:13:07 -0000

I guess Beth's allusion to Gouldner is a mangled fractional reference to
William Gould's FAQ as mentioned by Scott. 

Another way to do it: 

foreach var of varlist mem* {
	gen `var'_r =  2 - `var' if inlist(`var', 1, 2)
}

Nick 
n.j.cox@durham.ac.uk 

Scott Merryman

The problem is that `i' does not equal `n' -- as you have it `i' = 1
and `n' = 17 so the whole -while- loop bypassed.  If you -set trace
on- it will show the problem:

. while `i' == `n' {
  2.    local var1 : word `i' of `array1'
  3.    local var2 : word `i' of `array2'
  4.    replace `var1' = 1 if `var2' == 1
  5.    replace `var1' = 0 if `var2' == 2
  6.    local i = `i' + 1
  7. }
- while `i' == `n' {
= while 1 == 17 {
  local var1 : word `i' of `array1'
  local var2 : word `i' of `array2'
  replace `var1' = 1 if `var2' == 1
  replace `var1' = 0 if `var2' == 2
  local i = `i' + 1
  }

I suppose it should be -while `i' <= `n' {- ....

I believe the 30 lines of code you have could be reduced to 3 with:

foreach var of varlist mem* {
	gen `var'_r =  cond(`var' == 1, 1, cond(`var' == 2, 0,.))
	}


Also -findit sas array- points to the FAQ "How do I implement SAS-like
ARRAYs in Stata?" http://www.stata.com/support/faqs/data/arrays.html

On Fri, Mar 12, 2010 at 11:51 AM, Beth Fussell <befeff@gmail.com> wrote:

> I'm a new Stata user trying to convert my SAS programs. I've followed
> the model Gouldner provides on using SAS-like arrays but it isn't
working.
> I'm not getting any useful error messages either. Can someone identify
the
> problem in my code



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