Reputation: 1396
I have desire to create variable for binning data in SAS. However, this seems to be harder than one would assume.
For Example I would have:
var1
1
2
3
5
11
17
...
I want to have bins size of 3:
var1 bin_var
1 1
2 1
3 1
5 2
11 4
17 6
...
I've come up with a way to do this by looping over the whole range:
%do i=&bin_min %to &bin_max %by &bin_size;
data foobar;
set foobar;
if (&i =< var_to_bin < (&i+&bin_size)) then; bin_var=&i;
run;
%end;
However, this is very slow, not to mention just plain silly. Anyone know better way to accomplish this?
Upvotes: 1
Views: 665
Reputation: 63424
How about using simple division? You can add additional code to deal with bin_min/bin_max issues if you need to.
data want;
set have;
bin_var= ceil(var1/&bin_size.);
run;
Upvotes: 1
Reputation: 12465
Sure, you don't have to loop outside the Data Step, you can do that inside the Data Step and get the same result.
data foobar;
set foobar;
%do i=&bin_min %to &bin_max %by &bin_size;
if (&i =< var_to_bin < (&i+&bin_size)) then; bin_var=&i;
%end;
run;
Or you can just use a couple functions and forgo the loop all together.
data have;
input var1;
datalines;
1
2
3
4
5
6
7
15
17
28
21
;
%let bin_size=3;
%let bin_min=1;
%let bin_max=6;
data want;
set have;
bin = max(&bin_min,min(&bin_max,floor( (var1-1) /&bin_size)+1));
run;
Upvotes: 2