Hi,
I have following data
obs | A | B | C | D |
1 | Type 1 | B1 | C1 | 1 |
2 | Type 1 | B1 | C2 | 3 |
3 | Type 2 | B2 | C3 | 2 |
4 | Type 3 | B3 | C4 | 5 |
I need a list (proc print or proc repot) like below. The “summary” line should show number of unique observations for variable A, B and C as well as SUM value for variable D.
A | B | C | D |
Type 1 | B1 | C1 | 1 |
Type 1 | B1 | C2 | 3 |
Type 2 | B2 | C3 | 2 |
Type 3 | B3 | C4 | 5 |
3 | 3 | 4 | 11 |
Below simple code get me a little bit forward:
proc print data=test noobs;
var A B C D;
sum D;
run;
But what do I do for A, B and C? I need the number of unique observations for variable A, B and C.
Is this possible with proc print or proc report?
Thanks
Sure .
data have; input A & $ B $ C $ D ; cards; Type 1 B1 C1 1 Type 1 B1 C2 3 Type 2 B2 C3 2 Type 3 B3 C4 5 ; run; ods listing close; ods pdf file='x.pdf' style=sasweb; proc report data=have nowd; column a b c d; define a /display; define d /display; compute d; length x y z $ 400; if not find(x,a) then do; x=catx(' ',x,a); xx+1;end; if not find(y,b) then do; y=catx(' ',y,b); yy+1;end; if not find(z,c) then do; z=catx(' ',z,c); zz+1;end; sum+d; endcomp; compute after; line @1 xx yy zz sum; endcomp; run; ods pdf close; ods listing;
Xia Keshan
To get count of unique values you'll need at least one other step. Proc freq or proc means with levels or Proc SQL and select distinct can get the counts of unique values. Proc freq may be the best bet:
I see how you get all the numbers in your chart except for the count of 5 in the column for C...it looks like the number at the bottom of the report should be 4 for C??
cynthia
Hi Cynthia,
Yes, the number count for C should be 4.
Is it possible to use compute in proc report to get the count?
Sure .
data have; input A & $ B $ C $ D ; cards; Type 1 B1 C1 1 Type 1 B1 C2 3 Type 2 B2 C3 2 Type 3 B3 C4 5 ; run; ods listing close; ods pdf file='x.pdf' style=sasweb; proc report data=have nowd; column a b c d; define a /display; define d /display; compute d; length x y z $ 400; if not find(x,a) then do; x=catx(' ',x,a); xx+1;end; if not find(y,b) then do; y=catx(' ',y,b); yy+1;end; if not find(z,c) then do; z=catx(' ',z,c); zz+1;end; sum+d; endcomp; compute after; line @1 xx yy zz sum; endcomp; run; ods pdf close; ods listing;
Xia Keshan
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.