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);

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 593 views
  • 1 like
  • 3 in conversation