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 ;

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