BookmarkSubscribeRSS Feed
InfoGuru
Calcite | Level 5
This is a very stupid question for which i seem to have lost my code and forgotten the rest. if someone can help me great.
To uniquely count rows in a data set with duplicate primary keys e.g. Three rows with data with the same primary key and i want to count them 1,2,3.
2 REPLIES 2
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
DATA step logic (sorted input file) using a BY statement and IF FIRST._varname_ logic to increment a user SAS variable for all non-FIRST conditions and assign a value of 1 for the FIRST condition.

* input to next DATA step is sorted on _primary_key_variables_. ;
DATA _output2_;
set _input1_;
BY _primary_key_variables_;
IF FIRST._your_last_break_variable_ THEN DUPCOUNT = 0;
IF NOT (FIRST._your_last_break_variable_ AND LAST._your_last_break_variable_) THEN DUPCOUNT + 1;
RUN;

Scott Barry
SBBWorks, Inc.
mftuchman
Quartz | Level 8
Another method that may work if you just want the counts of each duplicate key tuple is to use PROC FREQ with the LIST option.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 1159 views
  • 0 likes
  • 3 in conversation