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

Hey guys, thanks for the time,

So not to confuse you, i will try to explain my issue.

I have a table that contains information about clients and i would like to choose the store with maximum purchases for each client

Client_IDStorePurchases
132
154
143
253
275
265
338
352
341
452
473
461
532
553
541
651
672
665
734
753
745
852
873
861

So, for the example above, i need to choose the store with the maximum purchases for my client.

I don't think i can use "top with ties" which seems to be the solution if i were using T-SQL but since my data are stored on SAS EG, i cannot do so.

Ideas, please?!

1 ACCEPTED SOLUTION

Accepted Solutions
Vince28_Statcan
Quartz | Level 8

Hi,

Fred's solution will return one record for each client_id, store pair and not one record per client_id. I believe you wanted a single store returned per client so here are 2 solutions viable. The sort+data step will return only a single record. The proc sql will return all stores that share the same maximum purchase assuming you would want more than one record if there was a tie for the maximum

data temp;

     input clientid store purchases;

     datalines;

1 1 100

1 2 155

1 3 155

2 3 222

2 2 234

2 4 111

;

run;

/* Soln 1, single return even when there are ties */

proc sort data=temp;

     by clientid purchases;

run;

data temp2;

     set temp;

     by clientid;

     if last.clientid then output;

run;

/* end of soln1 */

/* soln2, multiple returns when ties occur */

proc sql;

     select clientid, store, purchases

     from (

          select clientid, store, purchases, max(purchases) as maxpurchases

          from temp

          group by clientid

          )

     where purchases=maxpurchases;

quit;

Hope this helps.

View solution in original post

6 REPLIES 6
Amir
PROC Star

Hi,

To carry this out with a data step look into reading the data by Client_ID and using the retain statement to hold values of the Store and Purchases.

if this is the highest value for Purchases in this by group then save the values of Store and Purchases.

At the end of the by group you can output the Client_ID and the Store and Purchases values you have retained to your output data set.

I hope that gives you some clues.

Regards,

Amir.

Vince28_Statcan
Quartz | Level 8

Hi,

Fred's solution will return one record for each client_id, store pair and not one record per client_id. I believe you wanted a single store returned per client so here are 2 solutions viable. The sort+data step will return only a single record. The proc sql will return all stores that share the same maximum purchase assuming you would want more than one record if there was a tie for the maximum

data temp;

     input clientid store purchases;

     datalines;

1 1 100

1 2 155

1 3 155

2 3 222

2 2 234

2 4 111

;

run;

/* Soln 1, single return even when there are ties */

proc sort data=temp;

     by clientid purchases;

run;

data temp2;

     set temp;

     by clientid;

     if last.clientid then output;

run;

/* end of soln1 */

/* soln2, multiple returns when ties occur */

proc sql;

     select clientid, store, purchases

     from (

          select clientid, store, purchases, max(purchases) as maxpurchases

          from temp

          group by clientid

          )

     where purchases=maxpurchases;

quit;

Hope this helps.

M_A_C
Calcite | Level 5

Thanks Vince,

Soln1 was exactly what i was looking for.

Gotta automate a whole process to get to the study of product affinity so has to start with this Smiley Wink

Thanks again mate !

M_A_C
Calcite | Level 5

Thanks Amir, gave me clues as you said Smiley Happy vince ws kind enough to give me the solution lol

fred_major
Calcite | Level 5

Hi

Proc sql;

select Client_ID,Store,max(Purchases)as MaxPurchases

from yourtable

group by Client_ID,Store;

quit;

Fred

M_A_C
Calcite | Level 5

Thanks fred for the response.

The script you wrote does return a result for each (Client_Id, Store) Combination which isn't what i am looking for but the answer vince gave was just it.

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
  • 1006 views
  • 0 likes
  • 4 in conversation