Hi,
I am using SAS Enterprise Guide 5.1.
I currently have millions of rows and thousands of different ID's. I would like the following (only 1 ID for an example):
id | month | Year | Level | What I want |
A | 8 | 17 | 1 | 1 |
A | 9 | 17 | 0 | 0 |
A | 10 | 17 | 1 | 1 |
A | 11 | 17 | 1 | 1 |
A | 12 | 17 | 1 | 1 |
A | 1 | 18 | 0 | 0 |
A | 2 | 18 | 1 | 0 |
A | 3 | 18 | 0 | 0 |
A | 4 | 18 | 1 | 1 |
A | 5 | 18 | 1 | 1 |
A | 6 | 18 | 0 | 0 |
A | 7 | 18 | 1 | 1 |
A | 8 | 18 | 1 | 0 |
A | 9 | 18 | 0 | 0 |
A | 10 | 18 | 0 | 0 |
A | 11 | 18 | 1 | 1 |
A | 12 | 18 | 0 | 0 |
I would like my computation to begin from when the month equals 1 and the year is 18.
I want to look back at the previous 5 months (already ordered by this - so previous 5 rows) and to sum column 'Level'. If this is greater than or equal to 3 then the new column of 'Level' (row 6) should be zero, only if the current column 'Level' is 1. If the column 'Level' is 0, it is irrelevant. I would like this to keep moving down until the last month of 12 and year 18.
This is my code I have tried, but it didn't work:
data work.test;
set WORK.MONTHS;
lag_Level = lag(Level);
lag_Level2 = lag2(Level);
lag_Level3 = lag3(Level);
lag_Level4 = lag4(Level);
lag_Level5 = lag5(Level);
if month eq 18 then do;
if sum(lag_count,lag_count2,lag_count3,lag_count4,lag_count5,Level) ge 3 then prop_Level = 0;
end;
else prop_Level = Level;
run;
data WORK.SAMPLEDATA;
infile datalines dsd truncover;
input id:$1. month:BEST12. Year:BEST12. Level:BEST12.;
format month BEST12. Year BEST12. Level BEST12.;
datalines;
A 8 17 1
A 9 17 0
A 10 17 1
A 11 17 1
A 12 17 1
A 1 18 0
A 2 18 1
A 3 18 0
A 4 18 1
A 5 18 1
A 6 18 0
A 7 18 1
A 8 18 1
A 9 18 0
A 10 18 0
A 11 18 1
A 12 18 0
;;;;
Any help is greatly appreciated.
Thanks!