BookmarkSubscribeRSS Feed
lakshmiG
Obsidian | Level 7

Hi Friends, Good day!!!

 

I need to create a new variable called Signal and populate either '+' or '-' or '0'. The condition is, if ROC>HH then '+' and no prior '-' signals for past 50 days. Likewise, if ROC<LL then '-' and no prior '+' signal for past 50 days. ROC, HH and LL are separate variables and have numeric values.

My query is, signal is the variable we are populating, how can we check whether there is prior + or - signs. How should I understand this logic and start coding?

Is it ok to populate + and - using the 1st condition, ROC>HH or ROC<LL and then consider the second condition for every observation?

in brief, we have multiple stocks and each stock has historical dates to current date. for every observation, we have ROC from 22nd observation since it is ROC for N=22 periods, HH AND LL populated from 72nd observation as they are N=50 for ROC(22).

kindly guide to populate signals.

I have also attached 1 stock information from our dataset for clear understanding.

3 REPLIES 3
PGStats
Opal | Level 21

I tried to translate your requirement into a data step:

 


data want;
retain lastH lastL;
set HL; by symbol;
if first.symbol then do;
    lastH = '01JAN1900'd;
    lastL = '01JAN1900'd;
    end;
if missing(ROC) then signal = " ";
else if ROC > HH and intck("DAY", lastL, date) > 50 then do;
    signal = "+";
    lastH = date;
    end;
else if ROC < LL and intck("DAY", lastH, date) > 50 then do;
    signal = "-";
    lastL = date;
    end;
else signal = "0";
run;
PG
lakshmiG
Obsidian | Level 7
Hi PG Stats, I will look into this logic and apply in my program and let you know if its working. Thanks much for your timely help!!!
lakshmiG
Obsidian | Level 7
Based on your logic as example, I tried and applies the below logic, it works fine,
retain hhcnt llcnt;
/* Initialize and Simply giving Value for First 1 to 71 Obs */
if count >= 1 and count <= 71 then do ;
hhsig = ' ' ;
hhcnt = 0 ;
llsig = ' ' ;
llcnt = 0 ;
SIG = ' ' ;
end;
/* Obs 72 to 121 - Simply Assign */
else if count >= 72 and count <= 121 then do ;
hhsig = '0' ;
hhcnt = 0 ;
LLSIG = '0' ;
llcnt = 0 ;
SIG = '0' ;
end;
/* Obs 122 and above - Simply Assign */
else if count >= 122 then do;
if ROC > HH then do;
if (llcnt >= count - 50) then do ;
SIG = '0' ;
hhsig = '0' ;
end;
else if (llcnt < count - 50) then do ;
SIG = '+' ;
hhsig = '+' ;
hhcnt = count ;
end;
end ;
else if roc < ll then do ;
if (hhcnt >= count - 50) then do ;
SIG = '0' ;
llsig = '0' ;
end ;
else if (hhcnt < count - 50) then do ;
SIG = '-' ;
llsig = '-' ;
llcnt = count ;
end ;
end ;
else do ;
SIG = '0' ;
end ;
end ;

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
  • 3 replies
  • 1390 views
  • 1 like
  • 2 in conversation