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

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

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

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
  • 1078 views
  • 1 like
  • 3 in conversation