BookmarkSubscribeRSS Feed
Dogo23
Quartz | Level 8

Simple question probably, but I keep arranging and rearranging  distinct and group by in the code and I'm not getting anything different.

 

I have two tables that share an ID between the two of them. Let's say table a has revenue data associated with each ID and table two has an instance where two titles are associated with the same ID. I use a left join of table an and ask for a distinct ID, and even group by, but I keep getting two row back for that ID, one for each name from table 

 

proc sql;
create table tableau.metadata_match as 
select
distinct a.product_id,
a.region,
b.product_name,
a.transactions,
a.revenue
from table a
left join 
table b 
on a.product_id = b.product_id;
group by 1, 2 quit;

 

 

It returns these rows repeating the revenue. What I really want is only row for this.

product_idregionproduct_nametransactionsrevenue
54321SWproduct_a1256.11
54321SWproduct_b1256.11

 

 

sd

6 REPLIES 6
RahulG
Barite | Level 11

I see few problem with the sql query.

 

You should remove group by statement becuase I do not find any aggregate function being used in the select clause.

Then try again.

 

 

andreas_lds
Jade | Level 19

Which match in the second table should be selected?

 

Post some example input-datasets as data-steps, so that we see what you have.

Dogo23
Quartz | Level 8

Hi Andreas,

 

I posted fake data to show an example of the scenario and what output I'm receiving. However, in the real scenario, If I need to specify just one of these, how do I do it by choosing either randomly or based off of occurrence using a date field (first occurrence, most recent occurrence)?

FredrikE
Rhodochrosite | Level 12

The problem is that you still have product in your query and there are 2 different values which gives you 2 rows of data.

If you remove it you will get 1 row.

But if you still need product in your result you need to descide which one to keep, as Andreas says 🙂

//Fredrik

Dogo23
Quartz | Level 8

 If I need to specify just one of these, how do I do it by choosing either randomly or based off of occurrence using a date field (first occurrence, most recent occurrence)?

ballardw
Super User

I think that you may be misunderstanding what the DISTINCT actually attempts to do. It gets distinct combinations of ALL variables on the select statement not just the first variable.

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!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 6 replies
  • 8729 views
  • 0 likes
  • 5 in conversation