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

Hi All,

I have one sas dataset on which I want to create more than 10 continuous bins based on different criteria like for example:

for age <=18,18-25,25-50,>50

for balance <15000,15000-25000,25000-35000,>35000

for transactions <=5,5-10,10-15,>15 and so on.

 

I know this can be done using proc format and if then else statement.I was just wondering to create a macro to optimize this process by limiting the repetitive if then else condition.

 

Hence,I am looking for some inputs/ideas to start working on it.

 

Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

You could use an iterative DO statement with a list of values, as in this example:

 

data binned;
set sashelp.class;
ageBin = 0;
do i = 12, 14, 15;
    if age < i then leave;
    ageBin + 1;
    end;
heightBin = 0;
do i = 55, 60, 65;
    if height < i then leave;
    heightBin + 1;
    end;
weightBin = 0;
do i = 80, 87, 100;
    if weight < i then leave;
    weightBin + 1;
    end;
drop i;
run;

It is simple enough that a macro would seem like overkill.

PG

View solution in original post

3 REPLIES 3
PGStats
Opal | Level 21

You could use an iterative DO statement with a list of values, as in this example:

 

data binned;
set sashelp.class;
ageBin = 0;
do i = 12, 14, 15;
    if age < i then leave;
    ageBin + 1;
    end;
heightBin = 0;
do i = 55, 60, 65;
    if height < i then leave;
    heightBin + 1;
    end;
weightBin = 0;
do i = 80, 87, 100;
    if weight < i then leave;
    weightBin + 1;
    end;
drop i;
run;

It is simple enough that a macro would seem like overkill.

PG
Fabeeha
Fluorite | Level 6

interesting way of doing it.Thanks for sharing.

ballardw
Super User

Before even attempting any programming from a general rule you should get into a habit of thinking of which end points are inclusive or exclusive.

Everyone of your examples has shared endpoints. And in most of them I would not know in a report which category several values, such as age exactly = 25, the value is reported.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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
  • 2088 views
  • 2 likes
  • 3 in conversation