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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

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

View solution in original post

4 REPLIES 4
ballardw
Super User

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:

Cynthia_sas
Diamond | Level 26

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

alr
Quartz | Level 8 alr
Quartz | Level 8

Hi Cynthia,

Yes, the number count for C should be 4.

Is it possible to use compute in proc report to get the count?

Ksharp
Super User

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

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
  • 4 replies
  • 2284 views
  • 3 likes
  • 4 in conversation