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

# Re: st: egen anycount problem

 From Nick Cox To "statalist@hsphsun2.harvard.edu" Subject Re: st: egen anycount problem Date Fri, 17 May 2013 01:52:26 +0100

```Your central loop has the form

foreach v of varlist <varlist> {
egen missDS = ...
egen c08 = ...
}

I find it difficult to understand how that would work, as second time
around the the -egen- commands should fail because the new variables

However, your approach looks wrong in principle.

In looping over a varlist each time your -egen, anycount()- looks only
at a single variable given each time around as as argument.

The problem is not a looping problem, as that function takes a varlist
as argument, and that's its rationale. Nor need you worry about
missings. You are looking for incidences of 8 and missings are not a
problem, as they will be ignored.

Once you have numeric variables, courtesy of -destring-, a single call

egen c08 = anycount(destrprior_service_turps*), values(8)

By the way, having string variables is not an issue here. You can loop
over string variables like this

gen C08 = 0

quietly forval j = 1/25 {
replace C08 = C08 + (prior_service_turps_`j' == "8")
}

For more discussion, see

SJ-9-1  pr0046  . . . . . . . . . . . . . . . . . . .  Speaking Stata: Rowwise
(help rowsort, rowranks if installed) . . . . . . . . . . .  N. J. Cox
Q1/09   SJ 9(1):137--157
shows how to exploit functions, egen functions, and Mata
for working rowwise; rowsort and rowranks are introduced

A .pdf is available at http://www.stata-journal.com/sjpdf.html?articlenum=pr0046

Nick
[original author of -anycount()- in 1999 under the name of -neqany()-]
njcoxstata@gmail.com

On 17 May 2013 01:00, paul gross <paul@iheta.com> wrote:
> Dear statalisters
>
> Working with stata 12 and a large dataset of 2.2 million hospital admissions
> where  patients can have multiple or single admissions, I want to count how
> many GP visits (code 8) occurred in the 30 days prior to admission.I have up
> to 25 prior service indicators (prior_service_turps_1 –
> prior_service_turps_25) that can contain the type 8 ( or 15 other)  types of
> doctor encounter. I first destringed these 25 PSIs.
>
> My stata coding below  is intended to return for each admission the total
> number of code 8 in the 25 positions. I also am checking the total of
> missing values in the 25 positions as more missing codes is one of my
>
>
>
> destring prior_service_turps_1 -
> prior_service_turps_25,gen(destrprior_service_turps_1 -
> destrprior_service_turps_25)
>
> foreach v of varlist destrprior_service_turps_1 -
> destrprior_service_turps_25 {
>
> egen missDS=rowmiss(destrprior_service_turps_1 -
> destrprior_service_turps_25)
>
> egen  c08=anycount(`v') if `v' !=., values (8)
>
> }
>
> tab c08
>
> list obsno patno missDS c08 destrprior_service_turps_1 -
> destrprior_service_turps_3
>
>
> This coding produces the following values for patient no 18 , who clearly
> should have 3 code 8 recorded in variable c08, but has only 1
>
>
>      | obsno | patno | c08 | destr~_1 | prio~s_2 | destr~_2 | prio~s_3 |
> destr~_3 |
>
>      |    68      |    18   |   1   |        8         |      008     |
> 8        |      008     |        8
>
>
> Two probs.
>
> The c08 total is wrong.
> The list variables do not include the original stringed prior service codes,
> only the destringed versions, yet prio~s_2 and prio~s_3 are listed
>
> Have I misused the egen anycount specification?
>
> Paul Gross
> Sydney

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