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.
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;
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;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.