LISTSERV at the University of Georgia
Menubar Imagemap
Home Browse Manage Request Manuals Register
Previous messageNext messagePrevious in topicNext in topicPrevious by same authorNext by same authorPrevious page (August 2008, week 2)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:   Tue, 12 Aug 2008 18:52:01 -0400
Reply-To:   Arthur Tabachneck <art297@NETSCAPE.NET>
Sender:   "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:   Arthur Tabachneck <art297@NETSCAPE.NET>
Subject:   Re: Retaining RECORDS WITH DIFFERENCES LESS THAN 3
Comments:   To: Kwame Asom <asom77@HOTMAIL.COM>

Kwame,

Similar to Barry's suggestion you could also do something like:

data Ds1; input id aw; cards; 37 3 37 6 37 13 37 14 37 18 39 7 39 10 39 12 39 17 ;

data ds2 (keep=id aw difw); set Ds1; by id; hold_difw=ifn(first.id, (.), dif(aw)); last_aw=lag(aw); if hold_difw < 3 and not(missing(hold_difw)) then do; hold_aw=aw; call missing(difw); aw=last_aw; output; difw=hold_difw; aw=hold_aw; output; end; run;

HTH, Art --------- On Tue, 12 Aug 2008 15:40:24 -0700, barry.debenham@TALK21.COM wrote:

>On Aug 12, 4:19 pm, aso...@HOTMAIL.COM (Kwame Asom) wrote: >> Could someone help me with the SAS code to obtain ds2,please? Ie. to >> retain the ids and the aws, that will give me difw less than 3? I HAVE >> 100 DIFFERENT IDS, ONLY 2 ARE SHOWN. >> *Initially, I used the ifn and the dif functions to arrive at difw in ds1. >> This is the code I used to obtain difw: My ultimate aim is to arrive at >> ds2 below. >> Data w1; >> Set w; >> BY ID; >> Difw=ifn(first.id, (.), dif(aw)); >> Run; >> Ds1: >> Id aw difw >> 37 3 . >> 37 6 3 >> 37 13 5 >> 37 14 1 >> 37 18 4 >> 39 7 . >> 39 10 3 >> 39 12 2 >> 39 17 5 >> >> Ds2: >> Id aw difw >> 37 13 >> 37 14 1 >> 39 10 >> 39 12 > > > >An alternative might be to read the data twice in the same data step. >If I can assume that the data is sorted as in your example. > >Then:- > >Data W1(keep=id aw); > Set w(keep=id aw > rename=(aw=awf id=idf ) > firstobs=1); > Set w(keep=id aw > rename=(aw=awl id=idl ) > firstobs=2); > If ((awl - awf) lt 3) and (idf = idl) then > do; > aw = awf; > id = idf; > output; > aw = awl; > id = idl; > output; > end; >Run; > >This reads the data twice and renames the variables each time. Since >the first and second obs are read into the PDV at the same time you >can compare the difference directly. Combine this comparison with a >test to make sure that the id is the same across both observations >then output data for both of the records that meet the required >conditions. > >N.B. - This code is untested. > >If you haven't solved this yet I hope this code proves useful. > >Regards, > >BPD


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