Date: Fri, 16 May 2008 21:19:05 -0400 Hari Nath "SAS(r) Discussion" Hari Nath Re: question about creating a data subset To: Chang Chung

Thanks Chang. I was thinking of something like that. Good that 'do until' automatically retains values....

On Fri, 16 May 2008 16:13:08 -0400, Chang Chung <chang_y_chung@HOTMAIL.COM> wrote:

>On Fri, 16 May 2008 15:46:32 -0400, Hari Nath <hari_s_nath@YAHOO.COM> wrote: > >>Yet another solution using 'do until' trick which i learned from SAS- L.... >... >>data final (where = ( new in (1,2) ) drop=yes no ) ; >> do until (last.id) ; >> set practice_dates ; >> by id ; >> select(passed); >> when('Y') yes=1; >> when('N') no=1; >> otherwise; >> end ; >> end ; >> >> if yes and no then new=1 ; >> else if yes then new=2 ; >> else if no then new=3 ; >> >> do until (last.id) ; >> set practice_dates ; >> by id ; >> >> output ; >> end ; >>run ; > >hi, >here is a little bit more refined version. by the way, this way of using two >do until loops together is called the "Double DoW." In the first do until >loop we calculate some statistic about the by group and in the second one, >we use the statistic to output selectively. no need to retain or reset the >passedAny flag here. Can you see why? >cheers, >chang > >data practice_dates; > input id date mmddyy8. test passed \$; > format date mmddyy10.; > datalines; >1234 01021969 2345 Y >1234 01301969 3456 N >3157 02031969 2345 N >3157 02201969 2897 N >3157 04151969 2345 Y >1011 02051969 2345 N >1011 02211969 2345 N >1011 05201969 2897 N >2468 03211969 2234 Y >2468 07151969 2255 Y >; >run ; > >proc sort; > by id; >run; > >data final; > /* double DoW */ > do until (last.id); > set practice_dates; > by id; > if passed="Y" then passedAny=1; > end; > do until (last.id); > set practice_dates; > by id; > if passedAny then output; > end; > drop passedAny; >run; >/* check */ >proc print data=final; >run; >/* on lst >Obs id date test passed > 1 1234 01/02/1969 2345 Y > 2 1234 01/30/1969 3456 N > 3 2468 03/21/1969 2234 Y > 4 2468 07/15/1969 2255 Y > 5 3157 02/03/1969 2345 N > 6 3157 02/20/1969 2897 N > 7 3157 04/15/1969 2345 Y >*/

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