Help using Base SAS procedures

Percentile Buckets using Proc Rank

Accepted Solution Solved
Reply
Contributor
Posts: 39
Accepted Solution

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: 11,343

Re: Percentile Buckets using Proc Rank

Posted in reply to jacob_klimek

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;

View solution in original post


All Replies
Super User
Super User
Posts: 7,988

Re: Percentile Buckets using Proc Rank

Posted in reply to jacob_klimek
Super User
Super User
Posts: 7,988

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: 11,343

Re: Percentile Buckets using Proc Rank

Posted in reply to jacob_klimek

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.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 1377 views
  • 2 likes
  • 3 in conversation