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;*/
......
......
......
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;
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);
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.