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
SAS Super FREQ

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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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