Bookmark and Share

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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Arrays and loops for Stata

From   Stas Kolenikov <>
Subject   Re: st: Arrays and loops for Stata
Date   Fri, 13 Apr 2012 09:55:04 -0500

Instead of the long sequence of -replace- commands, you could

generate lastd = .
foreach k of numlist 14(-1)0 {
  replace lastd = disp_dat`k' if missing(lastd)

although I believe there is a better solution conceptually with some
sort of -bysort- statement for data in long form.

On Fri, Apr 13, 2012 at 9:48 AM, Hitesh Chandwani
<> wrote:
> Hello all,
> I'm using a dataset that I changed from long to wide using -reshape-.
> What I need to do is for Stata to run a loop that picks out the first
> date that a patient got a prescription from the variables
> disp_date0-disp_date14. I also need to run a loop that picks out the
> last date that the patient got a prescription. I figured out the long
> and crude way of doing this (given below) but would really appreciate
> it if someone could help me with the loop command.
> "disp_date0-disp_date14" are in chronological order.
> ***generate first date of dispensing drug***
> gen firstd=disp_date0
> replace firstd=disp_date1 if firstd==.
> ***generate last date of dispensing drug***
> gen lastd=disp_date14
> replace lastd=disp_date13 if lastd==.
> replace lastd=disp_date12 if lastd==.
> replace lastd=disp_date11 if lastd==.
> replace lastd=disp_date10 if lastd==.
> replace lastd=disp_date9 if lastd==.
> replace lastd=disp_date8 if lastd==.
> replace lastd=disp_date7 if lastd==.
> replace lastd=disp_date6 if lastd==.
> replace lastd=disp_date5 if lastd==.
> replace lastd=disp_date4 if lastd==.
> replace lastd=disp_date3 if lastd==.
> replace lastd=disp_date2 if lastd==.
> replace lastd=disp_date1 if lastd==.
> replace lastd=disp_date0 if lastd==.
> Thanks and regards,
> --
> Hitesh S. Chandwani
> University of Texas at Austin
> *
> *   For searches and help try:
> *
> *
> *

Stas Kolenikov, also found at
Small print: I use this email account for mailing lists only.

*   For searches and help try:

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