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

Greetings community

 

I am trying to work out a way to sum the number of unique values that occur for a variable in the dataset. So for example if the values for my observations were:

 

1

1

2

2

3

4

6

 

The answer would be 5 because there are 5 unique values. But I want to find this answer for each combination of two other variables in my dataset (type and version), so my data looks like this, where I am trying to count the number of unique instances of 'value':

 

ValueTypeVersion
11A
11A
21A
21A
31A
41A
61A
12A
12A
32A
32A
11B
31B
41B
51B
12B
22B
32B
42B
52B
62B

 

and my answer needs to look something like this:

 

 Version 
TypeAB
154
226

 

I think I can do this with Proc SQL but it has eluded me so far, any suggestions gratefully received!

 

Thanks

Andy

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
data a;
	input value type version $;
	cards;
1	1	A
1	1	A
2	1	A
2	1	A
3	1	A
4	1	A
6	1	A
1	2	A
1	2	A
3	2	A
3	2	A
1	1	B
3	1	B
4	1	B
5	1	B
1	2	B
2	2	B
3	2	B
4	2	B
5	2	B
6	2	B
;
run;

proc sql;
	select type,version,count(distinct value) from a group by type,version;
quit;

Now this does the calculations of the number of distinct levels of value, it does not re-arrange the results into a table with two rows (one for each type) like you show, but that's pretty simple to do in PROC REPORT if you really have to have it in that form.

--
Paige Miller

View solution in original post

3 REPLIES 3
PaigeMiller
Diamond | Level 26
data a;
	input value type version $;
	cards;
1	1	A
1	1	A
2	1	A
2	1	A
3	1	A
4	1	A
6	1	A
1	2	A
1	2	A
3	2	A
3	2	A
1	1	B
3	1	B
4	1	B
5	1	B
1	2	B
2	2	B
3	2	B
4	2	B
5	2	B
6	2	B
;
run;

proc sql;
	select type,version,count(distinct value) from a group by type,version;
quit;

Now this does the calculations of the number of distinct levels of value, it does not re-arrange the results into a table with two rows (one for each type) like you show, but that's pretty simple to do in PROC REPORT if you really have to have it in that form.

--
Paige Miller
AJChamberlain
Obsidian | Level 7

Thakns Paige, that's worked for me. A

 

novinosrin
Tourmaline | Level 20

Your sample output doesn't look like a dataset rather report with proc report/tabulate 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 1534 views
  • 0 likes
  • 3 in conversation