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

I would like to count value changes in a column like below:

ID VALUE COUNT
1 10 0
1 10 0
1 12 1
1 15 1

In this example ID and VALUE is my data and I want to make count column equal 1 each time the value change in given ID

I heard about lag function which could be helpful but I am not sure how to use it

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data have;
input ID	VALUE;
cards;
1	10	
1	10	
1	12	
1	15
;
data want;
 set have;
 by id value notsorted;
 count=first.value;
 if first.id then count=0;
run;

View solution in original post

2 REPLIES 2
Ksharp
Super User
data have;
input ID	VALUE;
cards;
1	10	
1	10	
1	12	
1	15
;
data want;
 set have;
 by id value notsorted;
 count=first.value;
 if first.id then count=0;
run;
FreelanceReinh
Jade | Level 19

Hi @PatrykSAS,


@PatrykSAS wrote:

I heard about lag function which could be helpful but I am not sure how to use it


Here's how the LAG function could be used:

data want;
set have;
count = value~=lag(value) & id=lag(id);
run;

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

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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