BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
jacob_klimek
Obsidian | Level 7

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.

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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

3 REPLIES 3
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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;
ballardw
Super User

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;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

What is Bayesian Analysis?

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 12858 views
  • 4 likes
  • 3 in conversation