Hello,
I have a dataset like this:
data have;
input day hw consec;
datalines;
1 1 1
2 1 2
3 1 3
4 0 0
5 0 0
6 1 1
7 1 2
8 0 0
9 1 1
10 0 0
;
run;
I am trying to create a new variable (max) that grabs the maximum consecutive count in a sequence. I created the consecutive variable hoping to do this but am having trouble figuring out the next step. Maybe that was not necessary and this can be done with just the hw variable.
My goal dataset looks like this:
data have;
input day hw consec max;
datalines;
1 1 1 3
2 1 2 3
3 1 3 3
4 0 0 0
5 0 0 0
6 1 1 2
7 1 2 2
8 0 0 0
9 1 1 1
10 0 0 0
;
run;
Thank you in advance.
data have;
input day hw consec;
datalines;
1 1 1
2 1 2
3 1 3
4 0 0
5 0 0
6 1 1
7 1 2
8 0 0
9 1 1
10 0 0
;
run;
data want;
if 0 then set have;
do max=1 by 1 until(last.hw);
set have;
by hw notsorted;
end;
do until(last.hw);
set have;
by hw notsorted;
max=max*(hw=1);
output;
end;
run;
data have;
input day hw consec;
datalines;
1 1 1
2 1 2
3 1 3
4 0 0
5 0 0
6 1 1
7 1 2
8 0 0
9 1 1
10 0 0
;
run;
data want;
if 0 then set have;
do max=1 by 1 until(last.hw);
set have;
by hw notsorted;
end;
do until(last.hw);
set have;
by hw notsorted;
max=max*(hw=1);
output;
end;
run;
This is perfect! Thank you very much. Can you please explain briefly how this works?
in a hurry to catch the train to go home . i will leave some notes in a while
Hi @pamplemousse822 Sorry for the delay. Here you go,
1. Hw is binomial event or in other words occurring as 1 or 0 aka True or False
2. The need for us is the count of continuous occurrences of true events
3. The data is very clear and simple in that sense, we can spot the sets of continuous occurrences of true events.
4. Logic is ---> Loop through the occurrences and count the sets of continuous occurrences of true events. Reset the false(untrue events) to zero.
5. First loop does the count determining the max as the stated objective and 2nd loop does resetting of untrue events
Overall, Boolean's, binomial stuff are my specialties. 🙂
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.