INDEPENDENT variables used with the dynamic prefix “&” are effective ONLY at compile time. When you issue a CALLNAT to a subprogram, it will execute the code compiled with whatever & values were in effect AT COMPILE TIME, not run time. It works dynamically with the program type only with the RUN command, which does a compile and run. A CALLNAT only does a “run” of the compiled subprogram code.
If you really need to do this, you could
- set a value for the independent variable in ‘program1’
- stack command ‘cat subprog’
- stack command ‘fetch program2’
- ‘program2’ does CALLNAT ‘subprog’ to the newly-compiled version of subprog, using the values of the independent variables set at compile time.
Do you really need the query to be dynamic? How many possible values will it take on? Could you use a DECIDE statement to choose one of the options (assuming you have various descriptors to read by) and the loop just do a PERFORM to operate on the view (“ART”)?
If this doesn’t give you some other approaches, please explain more about what you are attempting to do as dynamic programs are not a best practice for production programs.