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

# Re: st: RE: identifying highest number of consecutive variables where answer is consistent across observation

 From Alison El Ayadi To statalist@hsphsun2.harvard.edu Subject Re: st: RE: identifying highest number of consecutive variables where answer is consistent across observation Date Fri, 21 Feb 2014 14:24:36 -0800

```This worked perfectly.  Thank you very much, Joe!

Best,
Alison

On Fri, Feb 21, 2014 at 10:57 AM, Joe Canner <jcanner1@jhmi.edu> wrote:
> Alison,
>
> I think modifying the first -replace- statement to exclude missings should be sufficient:
>
> .  replace run=run+1 if  q`x'==q`y' & !mi(q`x')
>
> Regards,
> Joe
>
> -----Original Message-----
> From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Alison El Ayadi
> Sent: Friday, February 21, 2014 1:26 PM
> To: statalist@hsphsun2.harvard.edu
> Subject: Re: st: RE: identifying highest number of consecutive variables where answer is consistent across observation
>
> Thanks, Joe.
>
> This seems to work but it is also including missings as similars.  How would I modify this to exclude counting those that just have runs of missings?
>
> Thanks,
> Alison
>
> On Thu, Feb 20, 2014 at 10:09 AM, Joe Canner <jcanner1@jhmi.edu> wrote:
>> Alison,
>>
>> That's what I thought you meant at first, but then I started to wonder...
>>
>> Try something like:
>>
>> . gen run=0
>> . gen run15=0
>> . forvalues x=1/93 {
>> .    local y=`x'+1
>> .    replace run=run+1 if  q`x'==q`y'
>> .    replace run=0 if  q`x'!=q`y' & run15==0
>> .    replace run15=1 if run==14
>> . }
>>
>> The variable -run15- will equal 1 if there was a run of 15 consecutive matches.  The variable -run- can be dropped as it will simply indicate the size of the run as of the 93rd comparison (q93==q94).
>>
>> Note that the last statement in the loop only requires -run- to be 14 since 14 equal comparisons (q1==q2, q2==q3, q3==q4, ...q14==q15) are required to find a run of 15.
>>
>> Let me know if this doesn't work for you.
>>
>> Regards,
>> Joe
>>
>> -----Original Message-----
>> From: owner-statalist@hsphsun2.harvard.edu
>> [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Alison El
>> Sent: Thursday, February 20, 2014 12:28 PM
>> To: statalist@hsphsun2.harvard.edu
>> Subject: Re: st: RE: identifying highest number of consecutive
>> variables where answer is consistent across observation
>>
>> Thanks, Joe, for helping me clarify my query into the appropriate language!
>> When I say in a row I mean consecutively across an observation, based on the variable order that I have assigned previously.
>> I have a total of 94 response variables and am looking to identify those observations which have a constant response (whatever it is) across 15 or more of the variables, in order.
>> I hope this makes more sense - let me know if you need more detail.
>>
>> Best,
>> Alison
>>
>>
>> On Thu, Feb 20, 2014 at 9:11 AM, Joe Canner <jcanner1@jhmi.edu> wrote:
>>> Alison,
>>>
>>> When you say "in a row" do you mean "consecutively" or are you referring to rows in the Stata sense (all of the variables for an observation)?
>>>
>>> Regards,
>>> Joe Canner
>>> Johns Hopkins University School of Medicine
>>>
>>> -----Original Message-----
>>> From: owner-statalist@hsphsun2.harvard.edu
>>> [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Alison El
>>> Sent: Thursday, February 20, 2014 12:04 PM
>>> To: statalist@hsphsun2.harvard.edu
>>> Subject: st: identifying highest number of consecutive variables
>>> where answer is consistent across observation
>>>
>>> Hi -
>>>
>>> I am doing some data cleaning on survey data and am looking to identify observations where there are 15 or more of the same answers in a row (across the variables in current order).  All of the variables are string.  Does anyone have an easy automated way to do this?  I'm thinking that it could be done by generating a variable that provided the maximum number of same responses in a row, but have no idea how to code this.  Variables are q1 - q94, and all string.
>>>
>>> Any suggestions on efficiently writing this code would be greatly appreciated.
>>>
>>>
>>> Best,
>>> 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/
>>
>> *
>> *   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/
>
> *
> *   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/

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