Contributor
Posts: 28

# Base SAS, How to look prior values of newly creating variable?

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.

Posts: 5,529

## Re: Base SAS, How to look prior values of newly creating variable?

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
Contributor
Posts: 28

## Re: Base SAS, How to look prior values of newly creating variable?

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!!!
Contributor
Posts: 28

## Re: Base SAS, How to look prior values of newly creating variable?

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 ;
Discussion stats
• 3 replies
• 404 views
• 1 like
• 2 in conversation