## feeding data to macro

Solved
Super Contributor
Posts: 506

# feeding data to macro

Hi Everyone,

I have a quite headache problem. I try to simplify as much as I could.

I have a time series data and I analyze Rate of Change (ROC). At the beginning, I analyze ROC for a fixed window for the whole dataset, say 3 day. I wrote a Macro and I can change the window i=.

In my new job, the window to calculate ROC is not fixed and for each record, I have to calculate different ROC window. This window is reported in a variable names WINDOW.

What I see is that for each record, I will feed the window variable into the ROC macro code and this Macro will run and create 1 output for me. Then it move to the next record….

I don’t know how to do it and I really appreciate it if you could help me.

Many thanks.

HHC

data ROC;

input value window;

datalines;

2 2

3 3

99 4

4 6

5 8

6 9

7 10

8 5

6 5

7 8

8 1

3 4

9 6

0 9

6 4

4 8

9 9

8 3

7 3

4 4

5 7

4 5

32 2

21 1

23 2

4 3

5 4

6 6

7 8

8 2

8 4

96 3

;run;

*this code below works for a FIXED window (i);

%macro mr(i= );

*rate of change for each window;

data ROC; set ROC;

l&i=lag&i(value);

roc&i=abs(value/l&i-1);

run;

*calculate Moving percentile for each window based on the previous 500 record;

data ROC; set ROC;

drop aa1-aa20;

array a aa1-aa20;

retain aa:;

pct_45=largest((20-45/100+1),of a{});

pct_95=largest((20-95/100+1),of a{});

a{mod(_n_,20)}=roc&i;

run;

*assign percentile to each record;

data ROC; set ROC;

drop pct_25-pct_100 l&i;

if                roc&i>=pct_95 and value>=l&i then roc&i._rank=95;

else if      roc&i>=pct_45 and value>=l&i then roc&i._rank=45;

else if      roc&i< pct_45 and value>=l&i then roc&i._rank=44;

if                roc&i>=pct_95 and value<l&i then roc&i._rank=-95;

else if      roc&i>=pct_45 and value<l&i then roc&i._rank=-45;

else if          roc&i< pct_45 and value<l&i then roc&i._rank=-44;

run;

%mend;

%mr (i=3);

Accepted Solutions
Solution
‎07-27-2015 05:30 AM
Super User
Posts: 9,599

## Re: feeding data to macro

Hi,

I don't have time to work out what the program is doing here, however as a suggestion why try generating the code your require.  So from your base dataset:

data ROC;

input value window;

datalines;

2 2

3 3

;

run;

data _null_;

set rocc;

call execute('do some code here');

do i=1 to window;  /* add loop in for up to window times */

call execute('do the loop code here');

end;

call execute('finish off the code here');

run;

You could also call you macro with the above.  The reason being is that with basic datastep, you can pretty much create any type or structure of code you like.

All Replies
Solution
‎07-27-2015 05:30 AM
Super User
Posts: 9,599

## Re: feeding data to macro

Hi,

I don't have time to work out what the program is doing here, however as a suggestion why try generating the code your require.  So from your base dataset:

data ROC;

input value window;

datalines;

2 2

3 3

;

run;

data _null_;

set rocc;

call execute('do some code here');

do i=1 to window;  /* add loop in for up to window times */

call execute('do the loop code here');

end;

call execute('finish off the code here');

run;

You could also call you macro with the above.  The reason being is that with basic datastep, you can pretty much create any type or structure of code you like.

Super Contributor
Posts: 506