DATA Step, Macro, Functions and more

How to separate into two data sets by conditionally looping through variables?

Reply
New Contributor
Posts: 4

How to separate into two data sets by conditionally looping through variables?

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;*/

......

......

......

Super User
Posts: 2,061

Re: How to separate into two data sets by conditionally looping through variables?

Posted in reply to garfield83
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;
Super User
Posts: 6,935

Re: How to separate into two data sets by conditionally looping through variables?

Posted in reply to garfield83

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

Ask a Question
Discussion stats
  • 2 replies
  • 111 views
  • 1 like
  • 3 in conversation