BookmarkSubscribeRSS Feed
hhchenfx
Rhodochrosite | Level 12

Good morning,

I have the following dataset:

Time      Value    Critical_level      Exit_level           

1              3              .                 .

2              9              20                30

3              5              3                   8

4              21           .                      .

5              31           .                     .

6              34           .                      .

For Time=2, I get in at its value of 9 and track its performance over time. If future value gets above Exit_level of 30, I will exit.

The tricky part is that, if value reach Critical_level (20), Exit_level should be increased by 10%. So at Time=4, Value is above 20 and therefore Exit_level is now increased by 10% to 33. So I will exit at Time=6 rather than time=5.

Any helps is very much appreciated.

Thank you,

HHC

5 REPLIES 5
Haikuo
Onyx | Level 15

What is the deal of T=3? Is that a downward exit setting?

hhchenfx
Rhodochrosite | Level 12

Hi,

Yes, it is downward exit. After Sas finish observation 2, finding its exit, it move to obsetvation 3.

Thank you,

Hhc

hhchenfx
Rhodochrosite | Level 12

Hi,

Yes, it is downward exit. After Sas finish observation 2, finding its exit, it move to obsetvation 3.

Thank you,

Hhc

Haikuo
Onyx | Level 15

Since you haven't lay out downward rules, this is only for the upward scenario:

data have;

input Time Value Critical_level Exit_level ; 

cards;

1 3 . .

2 9 20 30

3 5 3 8

4 21 . .

5 31 . .

6 34 . .

;

data want;

  set have;

  retain up_cr up_ext up_ext1 0;

if _n_=2 then do; up_cr=Critical_level; up_ext=Exit_level; up_ext1=Exit_level*1.1;end;

if value>= up_ext and _n_>3 then flag='sell';

  if value>= up_cr then up_ext=up_ext1;

run;

Haikuo

hhchenfx
Rhodochrosite | Level 12

Thank you, Haikuo.

Actually, I should be more clear about the output file which should be the original file and added 2 more column: Time2 that exit (or sell) happen and Actual_Exit_value (or up_ext1 in your code).

Based on your suggestion, I try the below code and it works.

Now, I am thinking to move it 1 step forward, which is: When critical_level is reached, I should change both Exit_level (up_ext 1) and Critical_level itself by 10%. So the whole system will move up by 10%.

Any suggestion is welcome,

Thank you,

HHC

data have;

input Time Value Critical_level Exit_level ;

cards;

1 3 . .

2 9 20 40

3 5 20 28

4 21 . .

5 31 . .

6 50 . .

;run;

data want3;

  set have nobs=totalobs;

  retain up_cr up_ext up_ext1 ;

found=0;

do n=_n_+1 to totalobs until(found=1);

  set have (keep=time value rename=(time=time2 value=value2)) point=n;

  up_cr=Critical_level; up_ext=Exit_level; up_ext1=Exit_level*1.1;

  if value2>up_ext then do;

  found=1;

  end;

  if value>=up_cr then up_ext=up_ext1;

end;

run;

data want3; set want3;

drop found;

if critical_level=. then do;

  time2=.;

  value2=.;

  end;

run;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1306 views
  • 3 likes
  • 2 in conversation