## Compound Do loop help

Super Contributor
Posts: 506

# Compound Do loop help

Hi Everyone,

I am trying to calculate moving percentile for rate of change (value(i)/value(i-N)) for a number of windows.

So I use the following code with Do Loop. Without macro, the code itself works.

However, I am not sure what I did wrong with the second step Do loop.

Also, is that correct to add a %do loop/%end for each dataset or is there a better approach.

Thank you for your support.

HHC

data have;
input close;
datalines;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
run;

/*----------RATE OF CHANGE----------*/
%macro mr(start= , end= );
data have; set have;
%do i=&start %to &end;
l&i=lag&i(close);
ROC&i=abs(close/l&i-1); *rate of change for each window;
%end;
run;

*calculate Moving percentile for each window based on the previous 10 record;
data have; set have;
%do i=&start %to &end;
drop aa1-aa10;
array a{0:9} aa1-aa10;
retain aa:;
a{mod(_n_,10)}=ROC&i;
pct_25=pctl(25,of a{*}); pct_50=pctl(50,of a{*}); pct_75=pctl(75,of a{*});
%end;
run;

%mend;
%mr (start=2, end=5);

Super User
Posts: 8,125

## Re: Compound Do loop help

I did not totally follow your logic but ....

Using a macro loop makes some sense as you are using the macro logic to generate the names that you want to give to your new variables.

But once that is done I would assume that normal data step looping with appropriate ARRAY statement would let you loop over the actual values of those newly generated variables.

Super Contributor
Posts: 506

## Re: Compound Do loop help

Hi Tom,

I learn the moving percentile from this post

https://communities.sas.com/message/173630

There is whole bunch for error generated from that array statement.

I think it relates to the array in loop(??)

SCFX

NOTE: Line generated by the invoked macro "MR".
15         drop aa1-aa200;     array a{0:199} aa1-aa200;     retain aa:;     a{mod(_n_,200)}=ROC&i;     pct_25=pctl(25,of
-
124
15  !  a{*});    pct_50=pctl(50,of a{*});    pct_75=pctl(75,of a{*});
NOTE: Line generated by the invoked macro "MR".
17         drop aa1-aa200;     array a{0:199} aa1-aa200;     retain aa:;     a{mod(_n_,200)}=ROC&i;     pct_25=pctl(25,of
-
124
17  !  a{*});    pct_50=pctl(50,of a{*});    pct_75=pctl(75,of a{*});
NOTE: Line generated by the invoked macro "MR".
19         drop aa1-aa200;     array a{0:199} aa1-aa200;     retain aa:;     a{mod(_n_,200)}=ROC&i;     pct_25=pctl(25,of
-
124
19  !  a{*});    pct_50=pctl(50,of a{*});    pct_75=pctl(75,of a{*});

ERROR 124-185: The variable a has already been defined.

NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.HAVE may be incomplete.  When this step was stopped there were 0 observations and 12
variables.
WARNING: Data set WORK.HAVE was not replaced because this step was stopped.

WARNING: Data set WORK.HAVE was not replaced because this step was stopped.

Super User
Posts: 8,125

## Re: Compound Do loop help

Did you read the error message?  It says that the name A that you picked for your ARRAY has already been used as the name of an existing variable in the data step.

I get the same message in this simple data step where I attempt to define the same array twice.

1416  data x;

1417    array a a1-a5 ;

1418    array a a1-a5 ;

-

124

ERROR 124-185: The variable a has already been defined.

1419  run;

That is what I meant by NOT using the %DO loop to generate multiple SAS statements.  Instead just control the looping of your calculations using normal data step statements like DO.

Super User
Posts: 8,125

## Re: Compound Do loop help

If you have a data step that works without any macro logic then post that and explain what part of it you want to change.

Super Contributor
Posts: 506

## Re: Compound Do loop help

Thanks, Tom.

I see the problem.

At first I cannot imagine how it work and that's why cannot see why "a" is already defined.

Let me try to make an overall do loop rather than do loop within each data step.

SCFX

Super Contributor
Posts: 506

## Re: Compound Do loop help

Happy Father's Day Everyone!

Discussion stats
• 6 replies
• 705 views
• 0 likes
• 2 in conversation