Thanks for resurrecting this old thread because none of the answers address the real problem here.
The problem is one of not understanding the relationship between macro code and SAS code. Remember that the macro processor is used to generate text that the SAS processor can interpret as SAS code. The macro processor cannot understand dataset variables much less access their values. It cannot use SAS functions (unless you convert them to macro functions by using %SYSFUNC()).
In the original question the user tried to use the value of the data step variable I in a call to the macro function %SCAN(). But since the macro processor operates first and just generates text it did not see the letter I as a variable at all, but just as the letter I. That is why the %SCAN() function generated an error. Once they enclosed the code into a macro definition and used the %DO statement to create a macro variable (which they also named I) it could use the value of that macro variable as the argument in the %SCAN() function call. That way the macro do loop was able to generate a series of valid SAS assignment statements that SAS could then use to compile and run the data step.
Your proposed solution does not generate any errors, but really does not do anything useful. What it will do is create the dataset T2 as a copy of the existing dataset T. It will add an extra variable named I that will be 3 for every observation. It will also create a macro variable named VAR and a series of macro variables with names that follow the pattern xxx_LAG1 that have the text of a call to the LAG() function. It will recreate these macro variables over and over again as it processes each observation from the dataset T, but it never uses them (other than the one named VAR) for anything.
... View more