BookmarkSubscribeRSS Feed
Batman
Quartz | Level 8

The code below creates a unique count of the variable y for each value of x and a summary row.   However, it requires two queries and a data step.   Is there a way to all in one query?

 

/*sample data*/

data a;

input x $1. y 1.;

datalines;

 

a1

a2

a2

b1

b3

;

run;

 

proc sql;

/*first query*/

create table b as select x, n(distinct y) as y_cnt

from a

group by x;

/*second query*/

create table c as select 'Total' as x, n(distinct y) as y_cnt

from a;

quit;

 

data d;

length x $5;

/*append queries*/

set b c;

run;

 

1 REPLY 1
Reeza
Super User

Do your records overlap within X/ by group?

ie If you sum the total from the summary total are you expecting it to add to the total, or do you need a separate distinct count because of the overlap?

If you need that distinction I can't think of an alternative 😞

 

Hopefully someone else can, so partially responding to see if there is a solution to this problem. 

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
  • 1 reply
  • 535 views
  • 0 likes
  • 2 in conversation