Statalist The Stata Listserver

[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: RE: looping over bys groups?

From   Jeph Herrin <>
Subject   Re: st: RE: looping over bys groups?
Date   Sat, 04 Nov 2006 18:25:05 -0500

Thanks Nick, David, these were the kind of tips I was looking for. Don't
know why it didn't occur to me to reverse the sort order on date,  which
solves a number of problems, and confess I have only ever made cursory use of
postfiles, so didn't consider the usefulness here. I'll give it another go
and see what emerges.


David Elliott wrote:
I've done something very similar in the past only it was in a database
program. I sorted the data on patient in ascending order, but within
patient in descending date order so one worked toward the past as Nick
has suggested. What I did, however, was to write my results to a new
file as I scanned the old one. Stata would allow you to do this with
postfile where you would duplicate the information of the variables in
your original data and also add the derived variables for switch,
interval, etc., posted to the postfile.

In greater detail: Original file (A), postfile (B)

[commands to create a postfile]
forvalues i=1/`c(N)' {
[commands to read in A[`i'] variables]
local this_scrssn scrssn[`i'] //initialize scrssn
local j = `i'
while scrssn[`j']== `this_scrssn' & `j' <= `c(N)' { // while same individual
local ++j
[commands to read in A[`j'] variables]
[logic to compare `i'th prescription to `j'th prescription and
decide if add or switch]
[post to B]
continue //break out of while once we are sure about the index prescription

and so on. I have left it to you to fill in the [black boxes] (I'm
put in mind of this famous cartoon: ). I'd be
interested to see how this comes out.

* For searches and help try:

*   For searches and help try:

© Copyright 1996–2017 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index