Help using Base SAS procedures

Counting rows

Reply
N/A
Posts: 1

Counting rows

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.
Super Contributor
Super Contributor
Posts: 3,174

Re: Counting rows

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.
Contributor
Posts: 42

Re: Counting rows

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.
Ask a Question
Discussion stats
  • 2 replies
  • 132 views
  • 0 likes
  • 3 in conversation