Hi:
There are several ways to accomplish what you want. But without knowing what statements are being generated by the %times() macro program, it is a little iffy to speculate on the correct approach. The design of %times could have an impact on how you would call the macro program iteratively.
Also, I was a bit confused by your code...you show:
[pre]
data set temp;
[/pre]
Are you really creating 2 SAS datasets: WORK.SET and WORK.TEMP??? Does your macro program %times write to one or the other, depending on some logic????
Here are some questions to think about:
--- Are all the variables you want to process in SET SOURCE????
--- Are all the variables you want to process numbered variables?? START1, START2, START3, FINISH1, FINISH2, FINISH3, etc??? Do the variables have different names: START1, FRED, STVAR, FINISH1, ETHEL, FNVAR???
--- Does one observation contain all the variables???
--- Do you need to perform this calculation on every observation in the input dataset? Or do you need to conditionally perform this calculation?
One approach you might try to is determine whether multiple calls to the %times macro program give the results you desire in WORK.TEMP:
[pre]
data temp;
set source;
%times(start1, finish1, calcduration1);
%times(fred, ethel, calcduration2);
%times(stvar, fnvar, calcduration3);
%times(start4, finish4, calcduration4);
run;
[/pre]
If a test like the above works, and you get the desired results, then a simple data step DO loop and an ARRAY might work for your purposes.
If multiple calls to the %times macro program does NOT work to produce the results you want, then you may have to delve into what is happening in the %times macro in order to accomplish what you want.
On the other hand, if you want to vary the name of the output dataset or the input dataset, as well as the variable names you want to process, you might wish to
use a Macro %DO loop inside a new Macro program to generate multiple output datasets from multiple input datasets.
I always recommend this paper as a good starting point to understand, in general, how macro variables work and how macro programs work:
http://www2.sas.com/proceedings/sugi28/056-28.pdf
It should give you some ideas on macro basics. Then this blog posting on SAS Macro processing has a very simple example of a %DO loop that might be useful:
http://scott.sherrillmix.com/blog/programmer/sas-macros-letting-sas-do-the-typing/
Finally, the code below shows a very, very simple example of using a data step DO loop. Depending on the processing you want to do, this approach may or may not work for you. Note that my macro program, %DOCALC, is only generating 1 line of SAS code for every macro call.
Last, it would be remiss of me not to point out that IF all you want to do is calculate a new variable based on data step manipulations, you might wish to investigate PROC FCMP, which is a new feature of SAS that allows you to write a user-defined function, which you could then call from inside your data step program. If you have SAS 9.2, then PROC FCMP is available to you, as described here:
http://www2.sas.com/proceedings/forum2007/008-2007.pdf
cynthia
[pre]
** Define a Macro program to perform a calculation;
%macro docalc(arg1,arg2,arg3);
&arg3 = &arg1 * &arg2;
%mend docalc;
** Make some data;
data testvars;
infile datalines;
input name $ var1 var2 var3 hvar1 hvar2 hvar3;
return;
datalines;
alan 10 12 14 5 3 2
barb 15 10 5 5 4 3
carl 10 15 3 6 5 4
;
run;
** Call the macro program 3 different times;
data makenew;
length calc1 calc2 calc3 8.;
set testvars;
%docalc(var1, hvar1,calc1);
%docalc(var2, hvar2,calc2);
%docalc(var3, hvar3,calc3);
run;
proc print data=makenew;
title 'Calculate with multiple calls';
var name var1 hvar1 calc1 var2 hvar2 calc2 var3 hvar3 calc3;
run;
** Call the macro program inside a DO loop using ARRAY references;
data makenew_alt;
length calc1 calc2 calc3 8.;
set testvars;
array v var1-var3;
array h hvar1-hvar3;
array c calc1-calc3;
do i = 1 to 3 by 1;
%docalc(v(i), h(i), c(i));
end;
run;
proc print data=makenew_alt;
title 'Calculate using DO Loop and array';
var name var1 hvar1 calc1 var2 hvar2 calc2 var3 hvar3 calc3;
run;
[/pre]