BookmarkSubscribeRSS Feed
OLUGBOJI
Obsidian | Level 7

Please see below for values of what I have vs what I want. I want to get repeated consecutive names

for certain id's and their count and the cummulative count. Proccsql and sql if possible. Thanks

 

NameIDgroup id  
QSAVE40973  
OPEN40973  
OPEN40973  
QSAVE40973  
OPEN40973  
PASTECLIP81933  
MTEDIT40973  
GRIP_STRETCH81933  
CHPROP81933  
CHPROP81933  
MATCHPROP81933  
COPY81933  
TEXTEDIT81933  
TEXTEDIT81933  
MOVE81933  
GRIP_STRETCH81933  
MOVE81933  
ZOOM81933  
QSAVE40973  
OPEN40973  
PASTECLIP81933  
CHPROP81933  
CHPROP81933  
CHPROP81933  
CHPROP81933  
CHPROP81933  
CHPROP81933  
CHPROP81933  
CHPROP81933  
CHPROP81933  
MATCHPROP81933  
MATCHPROP81933  
CUTCLIP40973  
PASTEBLOCK81933  
CUTCLIP40973  
PASTEBLOCK81933  
     
     
WHAT I WANT    
     
NameIDgroup idrepeatcount
CHPROP819332 
CHPROP819332 
TEXTEDIT819332 
TEXTEDIT819332 
CHPROP819339 
CHPROP819339 
CHPROP819339 
CHPROP819339 
CHPROP819339 
CHPROP819339 
CHPROP819339 
CHPROP819339 
CHPROP819339 
MATCHPROP819332 
MATCHPROP819332 
2 REPLIES 2
jimbarbour
Meteorite | Level 14

@OLUGBOJI ,

 

This is reasonably straightforward if one uses BY processing with the NOTSORTED option.  See code, below, and, below the code, the results.

 

Regards,

 

Jim

 

DATA	Have;
	INFILE	DATALINES	DSD	DLM='09'X	MISSOVER;
	INPUT	Name	$
			ID	
			group_id
			;

DATALINES;
QSAVE	4097	3	 	 
OPEN	4097	3	 	 
OPEN	4097	3	 	 
QSAVE	4097	3	 	 
OPEN	4097	3	 	 
PASTECLIP	8193	3	 	 
MTEDIT	4097	3	 	 
GRIP_STRETCH	8193	3	 	 
CHPROP	8193	3	 	 
CHPROP	8193	3	 	 
MATCHPROP	8193	3	 	 
COPY	8193	3	 	 
TEXTEDIT	8193	3	 	 
TEXTEDIT	8193	3	 	 
MOVE	8193	3	 	 
GRIP_STRETCH	8193	3	 	 
MOVE	8193	3	 	 
ZOOM	8193	3	 	 
QSAVE	4097	3	 	 
OPEN	4097	3	 	 
PASTECLIP	8193	3	 	 
CHPROP	8193	3	 	 
CHPROP	8193	3	 	 
CHPROP	8193	3	 	 
CHPROP	8193	3	 	 
CHPROP	8193	3	 	 
CHPROP	8193	3	 	 
CHPROP	8193	3	 	 
CHPROP	8193	3	 	 
CHPROP	8193	3	 	 
MATCHPROP	8193	3	 	 
MATCHPROP	8193	3	 	 
CUTCLIP	4097	3	 	 
PASTEBLOCK	8193	3	 	 
CUTCLIP	4097	3	 	 
PASTEBLOCK	8193	3		
;
RUN;

DATA	Want;
	SET	Have;
	BY	Name	NOTSORTED;

	Repeat	+	1;

	IF	LAST.Name	THEN
		DO;
			OUTPUT;
			Repeat	=	0;
			DELETE;
		END;
	ELSE
		DO;
			DELETE;
		END;
RUN;

jimbarbour_0-1627063282076.png

 

mkeintz
PROC Star

@OLUGBOJI wrote:

Please see below for values of what I have vs what I want. I want to get repeated consecutive names for certain id's and their count and the cummulative count. Proccsql and sql if possible. Thanks

The moment you want to look for consecutive values in a data set, you should not be considering PROC SQL.  It's simply not meant to care about record order.  Yes, you can torture proc sql to do so, but it's just not worth it.  So @jimbarbour 's suggestion of the BY ... NOTSORTED; statement is the way to go.  It is exactly the right technique to detect and process repeated values.

 

I would suggest a simpler data step, however:

 

data want;
  set have ;
  by name notsorted;
  repeat+1;
  if first.name then repeat=1;
  if last.name;
run;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

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!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 615 views
  • 1 like
  • 3 in conversation