BookmarkSubscribeRSS Feed
sfan6
Calcite | Level 5

Hello everyone, I have the following data set,

 

Ticker       Year       Net income          Net income is negative in the past two years? (yes=1, no=0)

A              2000          2

A              2001           -1
A              2002           1                                                  0
A              2003            -1                                               0
A              2004             -3                                              1
A              2005             -2                                               1
B              2001              3               

B              2002               4

B              2003               -1                                             0

B              2004               -2                                              1

 

My question is how to construct "Net income is negative in the past two years", the last column?

2 REPLIES 2
PaigeMiller
Diamond | Level 26

Use the LAG() and LAG2() function. 

--
Paige Miller
Ksharp
Super User
data have;
input Ticker $   Year       Netincome ;
cards; 
A              2000          2
A              2001           -1
A              2002           1                                                  0
A              2003            -1                                               0
A              2004             -3                                              1
A              2005             -2                                               1
B              2001              3               
B              2002               4
B              2003               -1                                             0
B              2004               -2 
;

proc sql;
create table want as
select *,case when 
(select sum(netincome) from have 
where ticker=a.ticker and year between a.year-1 and a.year)<0 then 1
else 0 end as flag
 from have as a;
quit;

sas-innovate-white.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.

 

Early bird rate extended! Save $200 when you sign up by March 31.

Register now!

What is ANOVA?

ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 495 views
  • 0 likes
  • 3 in conversation