# AnalyticBridge

Subscribe to Vincent Granville's Weekly Digest:

# How to use 'do loops' for ranges in SAS?

Please may I kindly seek your input in solving a problem in SAS. I want this done asap, hope someone can response on time.

Essentially, I want to group a variable in different ranges, with incremental value of 5000. So, the iteration, if done manually, will take about 20,000 entries and thus I am trying to adopt a clever way to automate this. I am sure either some do loops; array or iterative do- statement can do this.

Here is an example:

if 5000 <= totprice <= 10000 then vband=1;

else if 10000 < totprice <= 15000 then vband =2;

else if 15000 < totprice <= 20000 then vband =3;

else if totprice > 20000 then vband =4;

proc format ;

value band

1='     5000'

2='    10000'

3='    15000'

4='    20000';

In the end, I will proc tabulate my table and the output will come up as formatted above with other stuff.

Any help will be appreciated.

Views: 80

### Replies to This Discussion

Do you know how to write a macro?

Something along these lines:

%macro bin;

data a;
set a;
if missing(totprice) then vband= .;
else if totprice<= 0 then vband= 0;
%do i = 1 %to 20000;
else if totprice<= %eval(&i. * 5000) then vband= &i.;
%end;
else vband = 20001;
run;

%mend;

%bin;

Hi Ajay,

Many thanks for your prompt response. I did tried to reply immediately but to no avail.

I have less expertise knowledge about macros but I quite understand the logic of your suggested code. However, it doesn't seem to work. When I copied & pasted it and do the obvious changes with data, some of the key functions aren't highlighted. Pls what am i doing wrong? Pls i will appreciate if you kindly provide more insight.

I look forward to hearing from you. Thanks a million!

1

2

3

4

5

6

7

8

9

10