Hi Jason. To confirm, you want to store DATA step code in a SOURCE block, then replace a specific part of that DATA step in the source block with the value of i? If I am understanding correctly, this code below should help.

A SOURCE block simply stores text. See this code:

* Notice that the source block simply stores a string in a variable *;
proc cas;
source ds_code_execute;
data casuser.test;
x=:value:;
output;
run;
endsource; print "-----ds_code_execute value------";
print ds_code_execute;
describe ds_code_execute;
quit;

Since the SOURCE block stores a string, you can use the tranwrd function to replace a string in the SOURCE block. The key is to making the string you want to replace unique. Here I made the string I want to be replaced called :value:.

I also noticed you used a macro loop. There is no need to do that in CASL. You can use the CASL loop.

proc cas;
source ds_code_execute;
data casuser.test;
x=:value:;
output;
run;
endsource;* No need for a macro loop. CASL has a looping feature *;

do i=1 to 5; * make the i variable a string *;
make_i_a_string = putn(i,'1.');
print char_var; * create a new variable with the data step code and replace whatever you want based on the unique string you used in the data step code *;
new_ds_code=tranwrd(ds_code_execute,':value:',char_var);
print '------------------------';
print new_ds_code; end;
run;