Solved
Contributor
Posts: 39

# Percentile Buckets using Proc Rank

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.

Accepted Solutions
Solution
‎11-02-2016 03:27 PM
Super User
Posts: 13,563

## Re: Percentile Buckets using Proc Rank

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;

All Replies
Super User
Posts: 9,599

Super User
Posts: 9,599

## Re: Percentile Buckets using Proc Rank

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;```
Solution
‎11-02-2016 03:27 PM
Super User
Posts: 13,563

## Re: Percentile Buckets using Proc Rank

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;

☑ This topic is solved.