# Re: st: RE: programming problem

 From Jeph Herrin To statalist@hsphsun2.harvard.edu Subject Re: st: RE: programming problem Date Fri, 17 Aug 2007 09:34:58 -0400

```Never noticed -concat- before. Nice.

thanks,
Jeph

Nick Cox wrote:
```
This problem is, in my view, an awkward little challenge.
I very nearly posted a solution that was _quite_ wrong.
A -reshape- solution, as exemplified by Sebastian's code,
looks awkward for just this problem, but may of course be a good
idea for a variety of other problems.
A quite different idea runs like this:
egen history = concat(var1-var12) gen byte mycond = substr(history, month, 1) == "1"
The appearance here of avoiding a loop over variables
is illusory, as a loop is hidden inside -concat()- (and even as of Stata 10 it is still a -while- loop).
But a concatenated history makes some other problems easy, even with this wide data structure.
Has this person experienced a spell of three or more months with this condition?
gen byte spell3 = strpos(history, "111") > 0
When was the first month in the year with this condition?
gen byte first = strpos(history, "1")
When was the last month in the year with this condition?
gen byte first = cond(first, 13 - strpos(reverse(history), "1"), 0)
Nick n.j.cox@durham.ac.uk
Jeph Herrin

```Duh.

forval j = 1/12 {
replace mycond =  `j' == month & var`j' == 1
}

Which effectively only tests month 12, before I veered
off in another direction.
```
Nick Cox

Short of a solution using -reshape-, I think
this will suit:
gen mycond = 0 forval j = 1/12 { replace mycond = 1 if `j' == month & var`j' == 1 }
where I assume that your -var*- are 1 when true.
Jeph Herrin

```I have observations on patient admissions over
a 12 month period.

For each observation, I have 12 variables, call
them var1-var12, which indicate whether a certain
condition was met for that patient for months 1-12.
So, eg, it may be true for months 1-3 and 7-12.

I also have a variable -month-, which indicates
the current month of the observed admission. What
I want to capture is whether the condition was true

For instance, at first I unthinkingly tried this:

gen mycond = var`=month'==1

but this of course uses the first observed value
of -month-, which happens to be 6, and only checks
-var6-.

I eventually got there by creating lots of vars, but is
there a more direct solution?
```
```*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/

```
```*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```