Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


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

Re: st: using arrays to make multiple indicator variables


From   Scott Merryman <[email protected]>
To   [email protected]
Subject   Re: st: using arrays to make multiple indicator variables
Date   Fri, 12 Mar 2010 13:07:25 -0600

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


Scott


On Fri, Mar 12, 2010 at 11:51 AM, Beth Fussell <[email protected]> wrote:
> Hi! 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?
>
> 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/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index