```Date: Tue, 20 Apr 2004 09:54:44 -0400 Reply-To: "Richard A. DeVenezia" Sender: "SAS(r) Discussion" From: "Richard A. DeVenezia" Subject: Re: summarizing data by case and each consecutive period Ed Heaton wrote: > Liping, > > You now have some DATA step solutions, but why write a program when > SAS has a procedure that will do what you want so well?! First, your > data... > > Data test ; > Input @1 case @6 visitDate date9. @16 visit ; > Cards4 ; > 1001 01jan2002 1 > 1001 02jan2002 1 > 1001 03jan2002 1 > 1001 04jan2002 . > 1001 05jan2002 . > 1001 06jan2002 1 > 1001 07jan2002 1 > 1002 01mar2002 1 > 1002 02mar2002 1 > 1002 03mar2002 . > 1002 04mar2002 . > 1002 05mar2002 . > 1002 06mar2002 1 > ;;;; > > Now a simple SUMMARY. > > Proc summary data=test( rename=( visit=visitFlag ) ) ; > By case visitFlag notSorted ; > Output > out=summarized( > drop= visitFlag _TYPE_ _FREQ_ > where=( not missing(visit) ) > ) > min(visitDate)=visitDate > max(visitDate)=visit_end > sum(visitFlag)=visit > ; > Run ; > > Ed > > Edward Heaton, SAS Senior Systems Analyst, > Westat (An Employee-Owned Research Corporation), > 1600 Research Boulevard, RW-3541, Rockville, MD 20850-3195 > Voice: (301) 610-4818 Fax: (301) 610-5128 > mailto:EdHeaton@Westat.com http://www.Westat.com > > Here is a solution if the variable visit in the original data is really the number of visits on a given day (limited sample data was none (.) or one (1)). data case; informat case 4. visitdate date9. visit 1. ; input case visitdate visit; format visitdate ddmmyy10. ; cards; 1001 01jan2002 1 1001 02jan2002 2 1001 03jan2002 1 1001 04jan2002 . 1001 05jan2002 . 1001 06jan2002 1 1001 07jan2002 1 1002 01mar2002 2 1002 02mar2002 1 1002 03mar2002 . 1002 04mar2002 . 1002 05mar2002 . 1002 06mar2002 1 ;;;; run; * presume sorted by case and date; data case2 / view=case2; set case; by case ; ldate = lag(visitdate); lvisit = lag(visit); if first.case then group=1; else if visitdate - ldate ne 1 then group+1; else if visit and lvisit=. then group+1; drop ldate lvisit; run; proc summary data=case2 ; by case group notSorted ; where not missing(visit); output out=summarized( drop= group _TYPE_ _FREQ_ ) min(visitDate)=visit_Date_Range_Start max(visitDate)=visit_Date_Range_End sum(visit)=Date_Range_Number_Of_Visits ; run ; -- Richard A. DeVenezia http://www.devenezia.com/downloads/sas/macros/ ```

Back to: Top of message | Previous page | Main SAS-L page