SAS Procedures

Help using Base SAS procedures
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Nikos
Fluorite | Level 6

Dear all,

I have a table that can by grouped by two variables (i.e. VARA and VARB).

How can I randomly select groups of observations by VARA and VARB rather than single random observations.

Thank you in advance

Best regards

Nikos

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

For example:

data test;

input varA varB x;

datalines;

1 1 1

1 1 2

1 2 3

1 2 4

2 1 5

2 1 6

2 3 7

2 3 8

;

/* Extract all unique combinations of grouping keys */

proc sort data=test out=keys nodupkeys; by varA varB; run;


/* Assign a random number to each key combination */

data ranKeys;

call streamInit(72564);

set keys;

ran = rand("UNIFORM");

keep varA varB ran;

run;


/* Order grouping key combinations by random variable */

proc sort data=ranKeys; by ran; run;


/* Select the desired fraction of key combinations (here 50%) */

data sampleKeys;

set ranKeys nobs=nobs;

if _n_ > nobs*0.5 then stop;

run;


/* Extract the data corresponding to selected key combinations */

proc sql;

create table want as

select test.*

from test natural join sampleKeys;

select * from want;

quit;

PG

PG

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

Select VARA and VARB randomly

then subset the data table based on the random choices of VARA and VARB

--
Paige Miller
PGStats
Opal | Level 21

For example:

data test;

input varA varB x;

datalines;

1 1 1

1 1 2

1 2 3

1 2 4

2 1 5

2 1 6

2 3 7

2 3 8

;

/* Extract all unique combinations of grouping keys */

proc sort data=test out=keys nodupkeys; by varA varB; run;


/* Assign a random number to each key combination */

data ranKeys;

call streamInit(72564);

set keys;

ran = rand("UNIFORM");

keep varA varB ran;

run;


/* Order grouping key combinations by random variable */

proc sort data=ranKeys; by ran; run;


/* Select the desired fraction of key combinations (here 50%) */

data sampleKeys;

set ranKeys nobs=nobs;

if _n_ > nobs*0.5 then stop;

run;


/* Extract the data corresponding to selected key combinations */

proc sql;

create table want as

select test.*

from test natural join sampleKeys;

select * from want;

quit;

PG

PG
Ksharp
Super User

PG,

After you get all unique combination of group variable, I think we could use proc selectsurvey to randomly select the group ? and merge them back to get what we need ? Your thought ?

Xia Keshan

PGStats
Opal | Level 21

I think you can do the whole thing with surveyselect, using the CLUSTER statement. - PG

PG
Nikos
Fluorite | Level 6

Dear PG,

Unfortunately I do not have proc selectsurvey.

Thank you

Nikos

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 2298 views
  • 6 likes
  • 4 in conversation