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!
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.
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.
interesting way of doing it.Thanks for sharing.
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 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.