BookmarkSubscribeRSS Feed
Rohit_1990
Calcite | Level 5
Hi Everyone,
In given dataset we have C1 as party and C2 as group name.
C1 C2
1 A
2 A
3 A
4 B
3 B
5 B
1 C
6 C

Now as we see we have multiple group assigned to different parties for eg party 1 is part of group of A and C as well.

Now let's say we have data as follow:

C1
1
2
3
4
5
6

We need to assign group names to each one of them but some parties have multiple group names I will assign it the max of group they are part of.

So it would be like this.
C1 C2
1 C
2 A
3 B
4 B
5 B
6 C

But in case if any group is singular then in that case the group value has to be updated to group value of the other elements of its previous group .

So party 2 was associated with 1 and 3 but now since now 1 has group value as C and 3 has B.

Hence group value must be B or C for pary 2.

So final data would be like this.

C1 C2
1 C
2 B or C
3 B
4 B
5 B
6 C


1 REPLY 1
Ksharp
Super User

If I understood right.

 

data x;
input C1 C2	$;
cards;
1 A
2 A
3 A
4 B
3 B
5 B
1 C
6 C
;
run;
proc sql;
create table temp as
select c1,max(c2) as c2,count(*) as n
 from x
  group by c1
   order by c1;
quit;
data want;
 set temp;
 length lag $ 40;
 retain lag;
 lag=lag(c2);
 if n=1 and _n_ ne 1 then do;
  if lag>c2 then c2=lag;
 end;
 drop n lag;
run;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 1 reply
  • 563 views
  • 0 likes
  • 2 in conversation