Help with finding the lowest value after passing above a level

Accepted Solution Solved
Reply
Super Contributor
Posts: 402
Accepted Solution

Help with finding the lowest value after passing above a level

Hi Everyone,

I am strunggling with the below problem and I apprecaite it if you could help me out.

My data reports the price for each day.

It also has the trackingdate variable that basically the date I start tracking price movement.

I have outlevel which mean I stop tracking if price hit that outlevel

and next1 and next2 next3 which are the 3 critical levels.

What I want to do is that: for each trackingdate, I want to find if it hit the Next1 level. If it hit, I want to track the Lowest price before price hit either (OutLevel or Next2)

The time line looks like that:

trackingstart  ..... Hit Next1  ......... the lowest ...........End when price hit either (OutLevel or Next2)

For example, for the first trackingdate, price hit Next1 on record2. And then it hit above Next2 at record5. So between record2 and 5, the lowest is 7.

If it hit Outlevel before Next1, simply move to next tracking date. The example is trackingdate 2.

My code work ok for the first 2 records of trackingdate. Then it starts messsing up. The Id even turn different on that trackingdate 3 and 4.

I have no idea how to fix it now.

Thanks for reading my post.

HHC

data have;
input price  trackingdate outlevel next1 next2 next3;
datalines;
7 1 4 20 30 40
20 . . . . .
9 . . . . .
7 . . . . .
35 . . . . .
15 . . . . .
31 10 40 50 60
22 . . . . .
2 . . . . .
30 . . . . .
50 . . . . .
5 . . . . .
7 1 10 20 35 50
11 . . . . .
15 1 8 20 30 40
30 . . . . .
50 . . . . .
5 . . . . .
;run;
        
data have; set have;
id=_n_;run;
           
*------------------------------------------------------------------------------------------------------
if price hit next1, start tracking the priceest value before it hit either:
next2 OR OutLevel
*------------------------------------------------------------------------------------------------------;

*found=1 means reach to a level and then drop to SL
*found=2 means we have a minprice;

data want;
drop i ;
set have nobs=nobs;
i+1;
if trackingdate^=. then do;

*minprice=0;

do j=i+1 to nobs until (found=1 or found=2);
set have(keep=id price rename=(id=id2 price=price2)) point=j;

*if Outlevel is hit, FIRST PRIORITY EXIT;
  IF price2<=outlevel then do;
   found=1;
   minprice=outlevel;
  end;


  IF price2>=next1 then do;
  *start tracking the lowest level;
  minprice=100000;
  minprice_id=0;
   set have nobs=nobs;
   j+1;
   do k=j to nobs until (found=1 or found=2);
   set have(keep=id  price rename=(id=id3 price=price3)) point=k;

    if minprice>price3 then do;
     minprice=price3; *record lowest value;
     minprice_id=id3;
    end;

    *Exit condition 1;
    if price3<outlevel then do;
     found=1;
     minprice=outlevel;
    end;

    *Exit condition 1;
    if price3>next2 then do;
     found=2;
    end;
   end;
  end;
end;
end;
run;

data want; set want;
drop price2 price2 price3 price3;run;


Accepted Solutions
Solution
‎08-14-2014 09:05 PM
Super Contributor
Posts: 402

Re: Help with finding the lowest value after passing above a level

*start tracking the lowest level;

  minprice=100000;

  minprice_id=0;

  /* set have nobs=nobs;

   j+1;

*/

   do k=j to nobs until (found=1 or found=2);

   set have(keep=id  price rename=(id=id3 price=price3)) point=k;

    if minprice>price3 then do;

View solution in original post


All Replies
Solution
‎08-14-2014 09:05 PM
Super Contributor
Posts: 402

Re: Help with finding the lowest value after passing above a level

*start tracking the lowest level;

  minprice=100000;

  minprice_id=0;

  /* set have nobs=nobs;

   j+1;

*/

   do k=j to nobs until (found=1 or found=2);

   set have(keep=id  price rename=(id=id3 price=price3)) point=k;

    if minprice>price3 then do;

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 1 reply
  • 136 views
  • 0 likes
  • 1 in conversation