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: RE: identifying highest number of consecutive variables where answer is consistent across observation


From   Nick Cox <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: st: RE: identifying highest number of consecutive variables where answer is consistent across observation
Date   Thu, 27 Feb 2014 09:10:51 +0000

The  easiest way I can think of doing this is

gen run=0
replace run_15=0
forvalues x=1/93 {
local y=`x'+1
capture replace run=run+1 if  q`x'==q`y' & !mi(q`x')
capture replace run=0 if q`x'!=q`y' & run_15==0
replace run_15=1 if run==14
}

That way, commands failing because either q`x' or q`y' doesn't exist
will just be ignored.

Another way to do it is

gen run=0
replace run_15=0
forvalues x=1/93 {
local y=`x'+1
capture confirm var q`x' q`y'
if _rc == 0 {
    replace run=run+1 if  q`x'==q`y' & !mi(q`x')
    replace run=0 if q`x'!=q`y' & run_15==0
    replace run_15=1 if run==14
}
}

Some reading up about -capture- and -confirm- would help here.

There is a big reservation, however. If you still wanted to check that
say -q3- and -q5- are the same when -q4- is omitted (I won't say
"missing"!), this code never makes that comparison. You would need
more radical rewriting, perhaps like this

unab qall : q*
tokenize "`qall'"
local nq : word count `qall'

gen run=0
replace run_15=0
forvalues x=1/`nq' {
local y=`x'+1
replace run=run+1 if  ``x''==``y'' & !mi(``x'')
replace run=0 if ``x''!=``y'' & run_15==0
replace run_15=1 if run==14
}

Incidentally, I don't understand why this is written as though -run-
didn't exist already but -run_15- does.
Nick
[email protected]


On 27 February 2014 04:03, Alison El Ayadi <[email protected]> wrote:
> Dear Statalisters -
>
> I have been using the below code to identify the runs of consecutive
> variables where answer is consistent across observations (my thanks to
> Joe Canner) -
>
> gen run=0
> replace run_15=0
> forvalues x=1/93 {
> local y=`x'+1
> replace run=run+1 if  q`x'==q`y' & !mi(q`x')
> replace run=0 if q`x'!=q`y' & run_15==0
> replace run_15=1 if run==14
> }
>
> I now am in the situation where I have received an updated dataset
> that excludes certain variables (q4 q33 and others).  I wonder if
> there is an easy way to tell this program to exclude certain values
> but still run consecutively across those variables that are present?
>
> Thank you!
> Alison
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/faqs/resources/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/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


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