BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
hhchenfx
Barite | Level 11

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;

1 ACCEPTED SOLUTION

Accepted Solutions
hhchenfx
Barite | Level 11

*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

1 REPLY 1
hhchenfx
Barite | Level 11

*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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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