BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
pamplemousse822
Obsidian | Level 7

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. 

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20

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;

View solution in original post

4 REPLIES 4
novinosrin
Tourmaline | Level 20

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;
pamplemousse822
Obsidian | Level 7

This is perfect! Thank you very much. Can you please explain briefly how this works? 

novinosrin
Tourmaline | Level 20

in a hurry to catch the train to go home . i will leave some notes in a while

novinosrin
Tourmaline | Level 20

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. 🙂

 

 

 

SAS Innovate 2025: Register Now

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!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 4 replies
  • 3230 views
  • 2 likes
  • 2 in conversation