BookmarkSubscribeRSS Feed
HKlaassen
Calcite | Level 5

, I want to send a survey to our customers based on a random selection, but i'am stuck.

The problem is that a customers can appear within multiple categories (like customerid 1 and 2 in the example below) what can lead to duplicates in the random selection

What i need is a example how to realise a random selection which selects random three customers a category without duplicaties.

I'm using Enterprise Guide 4.3

input example

category 1customerid
1
2
3
4
5
category 2customerid
1
2
8
7
10

output example

category 1customerid
1
2
3
category 28
9
7
3 REPLIES 3
Doc_Duke
Rhodochrosite | Level 12

One approach would be:

Assign a pseudo-random number between 0 and 1 (the RANUNI function) to every record.

Sort by customerid and that number (the category will now be in a random order within customerid).

Select the first of each customerid (now have one record per customerid).

Re-sort by category and the random number (the customerids will now be in a random order within category).

Select first 3 in each category.

Doc Muhlbaier

Duke

HKlaassen
Calcite | Level 5

Thanks for your reply.

I only have one more question; how do I select the first 3 rows by each category?

Harm Klaassen

Doc_Duke
Rhodochrosite | Level 12

Harm,

I would usually use a DATA step and a retain statement for that.  Something like

DATA want;

SELECT have;

BY category;

RETAIN counter;  DROP counter;

IF first.category THEN counter=0;

IF counter <=3 THEN OUTPUT;

RUN;

Doc

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
  • 3 replies
  • 1142 views
  • 1 like
  • 2 in conversation