BookmarkSubscribeRSS Feed
hhchenfx
Barite | Level 11

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
Barite | Level 11

Hi,

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

Thank you,

Hhc

hhchenfx
Barite | Level 11

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
Barite | Level 11

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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 5 replies
  • 527 views
  • 3 likes
  • 2 in conversation