Hi,
I am trying to create buckets (bins) based off of the deciles of a variable. I.e I want the first 10% to receive a 1, the 10-20% to receive a 2, etc.
Below is an example of what my data looks like:
ID Var
1 2
2 25
3 12
4 16
5 8
6 13
7 9
8 11
9 3
10 1
And I want:
ID Var Bucket
1 2 2
2 25 10
3 12 7
4 16 9
5 8 4
6 13 8
7 9 5
8 11 6
9 3 3
10 1 1
I have been trying to work through the syntax using proc rank because I think that's what I want but I just can't get it. I think I am getting frustrated too easily because when I do a similar procedure in Stata it is pretty simple to grasp.
Any help would be great, thank you.
The basic bit is going to look like this:
proc rank data=have out=want groups=10;
var var;
ranks bucket;
run;
However proc rank assigns 0 to the lowest rank so you get 0 to 9 as provided in the documentation.
So you put your output data set through a data step to add 1;
data want;
set want;
bucket = bucket+1;
run;
Would not proc surveyselect be a better option:
Just to add an example of surveyselect:
data have; do id=1 to 10; var=4; output; end; run; proc surveyselect data=have groups=10 seed=49201 out=RandomGroups noprint; run;
The basic bit is going to look like this:
proc rank data=have out=want groups=10;
var var;
ranks bucket;
run;
However proc rank assigns 0 to the lowest rank so you get 0 to 9 as provided in the documentation.
So you put your output data set through a data step to add 1;
data want;
set want;
bucket = bucket+1;
run;
Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.
Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.
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.