Date: Tue, 8 Sep 2009 07:52:17 -0400 Arthur Tabachneck "SAS(r) Discussion" Arthur Tabachneck Re: Circular Trading Logic Urgent To: Ashok Iyer

Ashok,

As Richard mentioned, the full task is definitely NOT trivial.

I think that the following provides the output you want, but only for the first circular trading that it finds. That is, if the first buyer (a) sells half of the stock to b and the other half to c, only the first track will be traced.

I don't know if a hash solution can simplify that part of the task.

Art

The code:

data have; infile datalines; input symbol seller_code \$ buyer_code \$ price quantity datetime ; informat datetime anydtdtm17.; date=datepart(datetime); time=timepart(datetime); format date ddmmyy8.; format time time8.; datalines; 100 a b 100 100 15OCT2008:15:30:00 100 d e 150 90 15OCT2008:10:45:50 100 b c 200 100 15OCT2008:15:30:00 100 e f 220 100 15OCT2008:15:30:00 100 f g 180 100 15OCT2008:15:30:00 100 c b 160 100 15OCT2008:15:30:00 100 l m 300 100 15OCT2008:15:30:00 100 p q 350 100 15OCT2008:15:30:00 100 m n 400 90 15OCT2008:15:30:00 100 n o 450 90 15OCT2008:15:30:00 100 o l 500 90 15OCT2008:15:30:00 ;

proc sort data=have; by symbol; run;

proc transpose data=have out=transposed; var seller_code buyer_code price quantity date time datetime; by symbol; run;

data want (keep=symbol Normal_Path price quantity date time datetime Group_Circular_Path); set transposed; array indata(*) \$ col:; array keepdata(7,32) \$20; array Sequence(32); retain keepdata; format Group_Circular_Path \$200.; if mod(_n_,7) then do i=1 to dim(indata); keepdata(mod(_n_,7),i)=indata(i); end; else do; do i=1 to dim(indata); keepdata(7,i)=indata(i); end; do i=1 to dim(indata); Group_Circular_Path=catx('-',keepdata(1,i),keepdata(2,i)); Counter=1; Sequence(Counter)=i; last_link=keepdata(2,i); do j=i to dim(indata); if keepdata(1,j) eq last_link then do; Group_Circular_Path=catx('-',Group_Circular_Path, keepdata(2,j)); Counter+1; Sequence(Counter)=j; last_link=keepdata(2,j);

*Found group circular path; if keepdata(1,i) eq keepdata(2,j) then do; do k=1 to Counter; Normal_Path= catx('-',keepdata(1,Sequence(k)),keepdata(2,Sequence(k))); price=keepdata(3,Sequence(k)); quantity=keepdata(4,Sequence(k)); date=keepdata(5,Sequence(k)); time=keepdata(6,Sequence(k)); datetime=keepdata(7,Sequence(k)); output; end; leave; end; end; end; end; end; run; -------- On Tue, 8 Sep 2009 15:44:43 +0530, Ashok R <r.ashokiyer@GMAIL.COM> wrote: