Do Loop to pass value to body of code

Accepted Solution Solved
Reply
Super Contributor
Posts: 420
Accepted Solution

Do Loop to pass value to body of code

Hi Everyone,

I want to run my code for different values of variable lookback using this rule
Do lookback=4 to 10 by 2;

I know that I can make a macro say %macro mymacro(lookback=) and then list the value.

This code is for demonstration purpose only.

I really want to make a DO to pass the value into the body of the code.

Could you please help me with that?

Thank you,

HHC

Do lookback=4 to 10 by 2;

data pfm; set pfm;
drop i j l1 h1;
lowest=1000;
highest=0;
i+1;
do j=i to i+ lookback;
set pfm (keep=low high rename =(low=l1 high=h1)) point=j;
if lowest>l1  then lowest=l1;
if highest<h1  then highest=h1;
end;
run;

data pfm; set pfm;
rename
lowest=lowest_lookback  *How can I turn it into number lookback?;
highest=highest_lookback;
run;
....


Accepted Solutions
Solution
‎12-01-2014 12:19 PM
Super Contributor
Posts: 420

Re: Do Loop to pass value to body of code

%macro test;

%do oldvar=2 %to 6 %by 2;

data test&oldvar;

set sashelp.class;

run;

%end;

%mend;

%test;

For discrete value

%let oldvar=4 5 20;

%macro test;

%do i=1 %to %sysfunc(countw(&oldvar));

%let newvar=%scan(&oldvar,&i);

data test&newvar;

set sashelp.class;

run;

%end;

%mend;

%test;

Message was edited by: Harry Ng

View solution in original post


All Replies
Super User
Posts: 11,343

Re: Do Loop to pass value to body of code

I'm not sure quite what you are attempting to actually accomplish. Are you trying to use the loop to call the two steps with data pfm within the loop?

If you post some example data, as data step code preferably, and what the desired output should be that would help.

Also for test purposes I would think very carefully about using the:

Data pfm; set pfm;

structure. Reusing the same data set before you have the results you need leads to difficulty determining where a re-assignment error occurs.

Solution
‎12-01-2014 12:19 PM
Super Contributor
Posts: 420

Re: Do Loop to pass value to body of code

%macro test;

%do oldvar=2 %to 6 %by 2;

data test&oldvar;

set sashelp.class;

run;

%end;

%mend;

%test;

For discrete value

%let oldvar=4 5 20;

%macro test;

%do i=1 %to %sysfunc(countw(&oldvar));

%let newvar=%scan(&oldvar,&i);

data test&newvar;

set sashelp.class;

run;

%end;

%mend;

%test;

Message was edited by: Harry Ng

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 176 views
  • 3 likes
  • 2 in conversation