BookmarkSubscribeRSS Feed
garfield83
Calcite | Level 5

I need to separate a variable into small groups, for example, besides missing and 0, keep every 100 as a small group, how to write a macro or do loop to do this?

/*data bucket1;*/

/*set rf.bucket1;*/

/*if t_amt_pd_inst_trd=. then group=1;*/

/*else if t_amt_pd_inst_trd=0 then group=2;*/

/*else if t_amt_pd_inst_trd>=1 and t_amt_pd_inst_trd<=100 then group=3;*/

/*else if t_amt_pd_inst_trd>=101 and t_amt_pd_inst_trd<=200 then group=4;*/

/*else if t_amt_pd_inst_trd>=201 and t_amt_pd_inst_trd<=300 then group=5;*/

/*else if t_amt_pd_inst_trd>=301 and t_amt_pd_inst_trd<=400 then group=6;*/

/*else if t_amt_pd_inst_trd>=401 and t_amt_pd_inst_trd<=500 then group=7;*/

/*else if t_amt_pd_inst_trd>=501 and t_amt_pd_inst_trd<=600 then group=8;*/

/*else if t_amt_pd_inst_trd>=601 and t_amt_pd_inst_trd<=700 then group=9;*/

/*else if t_amt_pd_inst_trd>=701 and t_amt_pd_inst_trd<=800 then group=10;*/

......

......

......

2 REPLIES 2
novinosrin
Tourmaline | Level 20
data have;
n=.;
output;
do n=0 to 1000;
output;
end;
run;

data want;
set have;
if  missing(n)  then do; group=1;output;end;
else if n in (0,1) then do;group+1;output;end;
else if mod(n,100)=0 then do;
output;group+1;end;
else output;
run;
Astounding
PROC Star

Your first few categories have to be written out (similar to what you did).  But the rest can be collapsed into a single statement:

 

if t_amt_pd_inst_trd=. then group=1;

else if t_amt_pd_inst_trd=0 then group=2;

else if t_amt_pd_inst_trd>=1 then group = 3 + int((t_amt_pd_inst_trd-1)/100);

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

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
  • 2 replies
  • 925 views
  • 1 like
  • 3 in conversation