Sample 24572: Assign values evenly throughout a BY-Group
Use BY-Group processing to assign a value to a variable so that within each BY-Group there is an equal number of the assigned values.
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
data test;
infile datalines truncover;
input branch num_b num_c custid $ b1 b2 b3;
datalines;
1111 2 8 A 12817 6886
1111 2 8 B 12817 6886
1111 2 8 C 12817 6886
1111 2 8 D 12817 6886
1111 2 8 E 12817 6886
1111 2 8 F 12817 6886
1111 2 8 G 12817 6886
1111 2 8 H 12817 6886
2222 3 11 N 12309 2209 6054
2222 3 11 O 12309 2209 6054
2222 3 11 P 12309 2209 6054
2222 3 11 Q 12309 2209 6054
2222 3 11 R 12309 2209 6054
2222 3 11 S 12309 2209 6054
2222 3 11 T 12309 2209 6054
2222 3 11 U 12309 2209 6054
2222 3 11 V 12309 2209 6054
2222 3 11 W 12309 2209 6054
2222 3 11 X 12309 2209 6054
;
proc sort data=test;
by branch;
run;
/* Assign B1-B3 to an array for ease of grouping and reference. Since NUM_B */
/* represents the number of variables in the array that are populated and */
/* NUM_C is the total number of observations in the BY-Group, CT is computed */
/* to know how many values from B1-B3 can be assigned per BY-Group. LEFTOVER */
/* is computed to know how many extra values exist after the values are assigned */
/* equally within the BY-Group. */
data test;
set test;
by branch;
retain leftover ct i custleft;
array banker(*) b1-b3;
if first.branch then do;
ct=int(num_c/num_b);
i=1;
custleft=num_c;
leftover=mod(num_c,num_b);
end;
/* ASSIGN is the variable that is created holding one of the array values. */
/* CT is the number of observations left to assign that value to. */
assign=banker(i);
ct=ct-1;
custleft=custleft-1;
/* If CT=0 then determine which category CUSTLEFT falls into and compute */
/* variables accordingly. */
if ct=0 then do;
if custleft gt leftover then do;
i=i+1;
ct=int(num_c/num_b);
end;
if custleft lt leftover then do;
i=i+1;
ct=1;
end;
if custleft eq leftover then do;
ct=1;
I=1;
end;
end;
/* Comment out to see these variables in the PROC PRINT as well */
drop leftover ct i custleft;
run;
proc print;
run;
These sample files and code examples are provided by SAS Institute
Inc. "as is" without warranty of any kind, either express or implied, including
but not limited to the implied warranties of merchantability and fitness for a
particular purpose. Recipients acknowledge and agree that SAS Institute shall
not be liable for any damages whatsoever arising out of their use of this material.
In addition, SAS Institute will provide no support for the materials contained herein.
Obs branch num_b num_c custid b1 b2 b3 assign
1 1111 2 8 A 12817 6886 . 12817
2 1111 2 8 B 12817 6886 . 12817
3 1111 2 8 C 12817 6886 . 12817
4 1111 2 8 D 12817 6886 . 6886
5 1111 2 8 E 12817 6886 . 6886
6 1111 2 8 F 12817 6886 . 6886
7 1111 2 8 G 12817 6886 . 6886
8 1111 2 8 H 12817 6886 . 12817
9 2222 3 11 N 12309 2209 6054 12309
10 2222 3 11 O 12309 2209 6054 12309
11 2222 3 11 P 12309 2209 6054 2209
12 2222 3 11 Q 12309 2209 6054 2209
13 2222 3 11 R 12309 2209 6054 2209
14 2222 3 11 S 12309 2209 6054 6054
15 2222 3 11 T 12309 2209 6054 6054
16 2222 3 11 U 12309 2209 6054 6054
17 2222 3 11 V 12309 2209 6054 12309
18 2222 3 11 W 12309 2209 6054 2209
19 2222 3 11 X 12309 2209 6054 6054
Use BY-Group processing to assign a value to a variable so that within each BY-Group there is an equal number of the assigned values.
| Type: | Sample |
| Topic: | SAS Reference ==> DATA Step Data Management ==> Manipulation and Transformation ==> BY-group processing
|
| Date Modified: | 2005-12-08 11:34:05 |
| Date Created: | 2004-09-30 14:08:55 |
Operating System and Release Information
| SAS System | Base SAS | All | n/a | n/a |