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

hello folks,

I am trying to assign each observation into 3 groups based on two variables (N size and score) so that each group has similar values in two variables ( e.i. average N size and score per group is similar). I have a small size mock up data shown below ( of course my real data is longer). How should I write a code for group assignment?

data1

idN sizescore
A10131
B30127
C4109
D1110
E9125
51127
G22119
H42130
I7100
1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

I took code from @Rick_SAS  . But best choice is SAS/OR .

 


data Units;
infile cards expandtabs;
input id $  size	score;
cards;
A	10	131
B	30	127
C	4	109
D	1	110
E	9	125
F 	51	127
G	22	119
H	42	130
I	7	100
;


%let NumGroups =3;           /* number of treatment groups */
data Treatments;
do Trt = 1 to &NumGroups;    /* Trt is variable that assigns patients to groups */
   output;
end;
run;

%let Var = size score;      /* names of multiple covariates */
proc optex data=Treatments seed=97531 coding=orthcan;
   class Trt;
   model Trt;              /* specify treatment model */
   blocks design=Units;    /* specify units */
   model &Var;             /* multiple covariate means will be approx same */
   output out=Groups;      /* merged data: units assigned to groups */
run;

proc means data=Groups mean std;
  class Trt;
  var &Var;
run;

Ksharp_0-1644557111080.png

 

View solution in original post

8 REPLIES 8
PaigeMiller
Diamond | Level 26

PROC FASTCLUS

 

or

 

PROC CLUSTER with method = AVERAGE

--
Paige Miller
leex1514
Calcite | Level 5

It does not look like either method brings me similar average values in each variable. Is there any method how to assign each observation into 5 groups so that each group has similar average of v1 and v2?

PaigeMiller
Diamond | Level 26

Not sure exactly what you mean by "similar". Can you show us (a portion) of the clusters that result?

--
Paige Miller
leex1514
Calcite | Level 5
For 3 groups i would like to assign, I hope to see each group's mean of N size and mean of score same (similar if not same): group 1 average N size =50 average score=60, group 2 average N size =50 average score=60,, group 3 average N size =50 average score=60 something like this. I am aware the averages can not be exactly same among groups but similar means within one or two points difference.
Ksharp
Super User

I think it is more like a OR problem.

Post it at OR froum

https://communities.sas.com/t5/Mathematical-Optimization/bd-p/operations_research

 

and calling out @RobPratt 

 

here is @Rick_SAS blog about it ,maybe could give you a  help.

https://blogs.sas.com/content/iml/2017/05/01/split-data-groups-mean-variance.html

 

 

RobPratt
SAS Super FREQ

Please see my answer to this earlier question for an approach that uses PROC OPTMODEL.

leex1514
Calcite | Level 5
Thank you RobPratt. This solution works as well.
Ksharp
Super User

I took code from @Rick_SAS  . But best choice is SAS/OR .

 


data Units;
infile cards expandtabs;
input id $  size	score;
cards;
A	10	131
B	30	127
C	4	109
D	1	110
E	9	125
F 	51	127
G	22	119
H	42	130
I	7	100
;


%let NumGroups =3;           /* number of treatment groups */
data Treatments;
do Trt = 1 to &NumGroups;    /* Trt is variable that assigns patients to groups */
   output;
end;
run;

%let Var = size score;      /* names of multiple covariates */
proc optex data=Treatments seed=97531 coding=orthcan;
   class Trt;
   model Trt;              /* specify treatment model */
   blocks design=Units;    /* specify units */
   model &Var;             /* multiple covariate means will be approx same */
   output out=Groups;      /* merged data: units assigned to groups */
run;

proc means data=Groups mean std;
  class Trt;
  var &Var;
run;

Ksharp_0-1644557111080.png

 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

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

Browse our catalog!

Discussion stats
  • 8 replies
  • 2633 views
  • 8 likes
  • 4 in conversation