```Date: Fri, 28 Jan 2005 23:52:18 -0500 Reply-To: "Howard Schreier " Sender: "SAS(r) Discussion" From: "Howard Schreier " Subject: Re: How to classified a series of observation ? Here is a refinement of Ya's approach. It gets rid of the wallpaper and generates the CAT values internally. data xx; input id (t1 t2 t3 t4 t5 t6 t7 t8 t9)(\$); cards; 1 3 3 3 3 3 3 3 3 3 2 1 1 3 3 3 3 3 3 3 3 2 2 2 2 2 1 1 1 1 4 1 1 1 3 3 3 3 3 2 5 2 1 1 1 1 1 1 3 3 6 2 2 2 2 2 2 2 2 3 7 1 1 1 1 1 1 1 1 1 8 2 1 1 1 1 1 1 1 1 9 1 1 3 3 3 3 1 1 1 10 1 1 1 1 1 1 1 1 3 11 1 2 3 2 1 2 3 2 1 ; data yy; set xx; length allt \$9; allt=compress(t1||t2||t3||t4||t5||t6||t7||t8||t9); do i=2 to 9; drop i; if substr(allt,i-1,1)=substr(allt,i,1) then substr(allt,i-1,1)=' '; end; allt=compress(allt); howlong = length(allt); run; proc sort data=yy; by howlong allt; run; data zz; set yy; by howlong allt; if first.allt then cat ++1; run; proc sort data=zz; by id; run; The string length is used as the major sort key so that the steady-state series get the lowest CAT values, followed by the series with only one state change, etc. On Wed, 26 Jan 2005 02:02:54 -0500, Ya Huang wrote: >Here is a revised shorter version: > >data xx; > set xx; >length allt \$9; >allt=compress(t1||t2||t3||t4||t5||t6||t7||t8||t9); >do i=2 to 9; >if substr(allt,i-1,1)=substr(allt,i,1) then substr(allt,i-1,1)=' '; >end; >allt=compress(allt); > if allt='1' then cat=1; >else if allt='2' then cat=2; >else if allt='3' then cat=3; >else if allt='132' then cat=7; >else if allt='131' then cat=9; >else if allt='213' then cat=8; >else if allt='13' then cat=4; >else if allt='21' then cat=5; >else if allt='23' then cat=6; >drop allt; >run; > >proc print noobs; >run; ```

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