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

I have a dataset that looks like this:

obs ID1 ID2 ID3

1      X     Y     Z

2      A     B     C

3      D     D     E

4      M     N     N

etc.

 

I'd like to produce a new variable that counts the number of unique strings across the ID variables. So it would produce this:

obs ID1 ID2 ID3 Count

1      X     Y     Z   3

2      A     B     C   3 

3      D     D     E   2

4      M     N     N   2

 

 

Suggestions?

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

1. Sort the array

2. Loop over array and check if it matches the previous value. 

 

If you don't want to sort your data you can copy the array to another temporary array and drop it. 

 

data have;
input obs ID1 $ ID2 $ ID3 $;
cards;
1      X     Y     Z
2      A     B     C
3      D     D     E
4      M     N     N
;
run;

data want;
set have;

array _id(3) id1-id3;

call sortc(of _id(*));
count=1;
do i=2 to dim(_id);
if _id(i) ne _id(i-1) then count+1;
end;

run;

Another solution is to transpose your data to a long format and use PROC SQL with COUNT DISTINCT or a double PROC FREQ.

An example of that is here:

https://github.com/statgeek/SAS-Tutorials/blob/master/count_distinct_by_group

View solution in original post

3 REPLIES 3
Reeza
Super User

1. Sort the array

2. Loop over array and check if it matches the previous value. 

 

If you don't want to sort your data you can copy the array to another temporary array and drop it. 

 

data have;
input obs ID1 $ ID2 $ ID3 $;
cards;
1      X     Y     Z
2      A     B     C
3      D     D     E
4      M     N     N
;
run;

data want;
set have;

array _id(3) id1-id3;

call sortc(of _id(*));
count=1;
do i=2 to dim(_id);
if _id(i) ne _id(i-1) then count+1;
end;

run;

Another solution is to transpose your data to a long format and use PROC SQL with COUNT DISTINCT or a double PROC FREQ.

An example of that is here:

https://github.com/statgeek/SAS-Tutorials/blob/master/count_distinct_by_group

TechRA
Calcite | Level 5

thank you! this helps because I wanted to also create some flags based on the uniqueness conditions. these will be easy to add to this set up.

Ksharp
Super User
It is very easy for IML code due to the function  COUNTUNIQUE().
Or Hash Table can do it though.



data have;
input obs (ID1 ID2 ID3) ($);
cards;
1      X     Y     Z
2      A     B     C
3      D     D     E
4      M     N     N
;
run;

data want;
 if _n_=1 then do;
  length k $ 200;
  declare hash h();
  h.definekey('k');
  h.definedone();
 end;
set have;
array x{*} $ id1-id3;
do i=1 to dim(x);
 if not missing(x{i}) then do;k=x{i};h.ref();end;
end;
count=h.num_items;
h.clear();
drop i;
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
  • 3 replies
  • 609 views
  • 0 likes
  • 3 in conversation