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

 

 

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.

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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;

View solution in original post

2 REPLIES 2
ballardw
Super User

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;

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
  • 4704 views
  • 2 likes
  • 2 in conversation