Solved
Contributor
Posts: 30

# Calculating and retaining the 'count of change of a value' within the group

I would like a variable, say group_count to show and accumulate a count if there is a change from previous value within the group. I want the count of names(in the following data) and the number of times it changed from one fmt to other

For example, I have a dataset ,

``````data comm;
input var1 name\$ fmt\$;
datalines;
1 A other
1 B other
1 C c
1 D d
2 A other
2 B c
2 C b
2 D c
3 A a
3 B c
3 C d
3 D c
4 A c
4 B other
4 C other
4 D b
;
run;

data test_comm;
set comm;
by var1;
retain group_change;
if first.var1 then
group_change=0;
if lag(val_fmt)=val_fmt then group_change=0;
else group_change+1;
if last.var1 then output;
run;``````

I want the output to be

var1 names group_Change

1    4      2

2        4           3

3        4           3

4        4           2

As 1 changed from other to c and c to d, the count of change is 2

As 2 changed from  other to c to b to c the count of changes is 3..

With my code, I get the group_Changes as 2,4,4,1 respectively . Also, I cannot figure out how to maintain the count of names.

Accepted Solutions
Solution
‎09-27-2016 02:32 PM
Super User
Posts: 13,563

## Re: Calculating and retaining the 'count of change of a value' within the group

Here's one approach.

Note change in variable to FMT from val_fmt in your code.

IF and LAG in the same statement seldom work as intended. Please spend some time reading the documentation about the separate streams. Then read again. Generally to mimimize debugging time just assign the LAGged variables for every observation.

You also needed another variable to accumulate the name count.

``````data comm;
input var1 name\$ fmt\$;
datalines;
1 A other
1 B other
1 C c
1 D d
2 A other
2 B c
2 C b
2 D c
3 A a
3 B c
3 C d
3 D c
4 A c
4 B other
4 C other
4 D b
;
run;

data test_comm;
set comm;
by var1;
retain group_change Names;
LagFmt = lag(Fmt);

if first.var1 then do;
group_change=0;
Names=1;
end;
Else do;
if LagFmt ne fmt then group_change+1;
Names+1;
end;
if last.var1 then output;
Keep var1 names group_change;
run;``````

All Replies
Solution
‎09-27-2016 02:32 PM
Super User
Posts: 13,563

## Re: Calculating and retaining the 'count of change of a value' within the group

Here's one approach.

Note change in variable to FMT from val_fmt in your code.

IF and LAG in the same statement seldom work as intended. Please spend some time reading the documentation about the separate streams. Then read again. Generally to mimimize debugging time just assign the LAGged variables for every observation.

You also needed another variable to accumulate the name count.

``````data comm;
input var1 name\$ fmt\$;
datalines;
1 A other
1 B other
1 C c
1 D d
2 A other
2 B c
2 C b
2 D c
3 A a
3 B c
3 C d
3 D c
4 A c
4 B other
4 C other
4 D b
;
run;

data test_comm;
set comm;
by var1;
retain group_change Names;
LagFmt = lag(Fmt);

if first.var1 then do;
group_change=0;
Names=1;
end;
Else do;
if LagFmt ne fmt then group_change+1;
Names+1;
end;
if last.var1 then output;
Keep var1 names group_change;
run;``````
Contributor
Posts: 30

## Re: Calculating and retaining the 'count of change of a value' within the group

Thanks! This worked.
☑ This topic is solved.