05-18-2017 02:57 AM
On each iteration of the data step, the macro variable should change it's value to the current value of a column in the data and then use it in further calculations. The example below shos what I would like to do - how do I set the value of 'macrovar'? This example is overly simplified, the real script I am working on doesn't really have any other solution (that I can think of).
data have; input Var1 Var2; datalines; 4 10 7 20 9 30 3 40 2 50 5 60 run; data want; set have; call symputx('macrovar',Var1); Var3 = Var2 + ¯ovar. ; /*equivalent of Var3 = Var2 + Var1*/ run;
05-18-2017 03:04 AM - edited 05-18-2017 03:04 AM
I have to ask, why not just
data have; input Var1 Var2; datalines; 4 10 7 20 9 30 3 40 2 50 5 60 ; data want; set have; Var3 = Var2 + Var1; run;
05-18-2017 03:06 AM
05-18-2017 04:00 AM
Ok, if you are not keen on explaining the real situation, here is the answer for your question. However, folks would be more interested to help and solve real problems.
input Var1 Var2;
Var3 = Var2 + resolve('¯ovar') ; /*equivalent of Var3 = Var2 + Var1*/
05-18-2017 04:50 AM
Then please post your real problem as what you have currently posted makes no sense, it is going against everything you should have learnt about Base SAS and Macro SAS. If something should "happen on every line of a datastep" - this this should be coded into a datastep. Macro SAS does nothing on its own, it is nothing more than a tool to create some text. The way you are presenting it in this post is that you are mixing Base and Macro which are two very different systems, so whichever way you try it you will likely fail. If you can post your actual problem there will likely be a very simple explanation.
05-18-2017 04:54 AM
05-18-2017 06:45 AM
Because it would not solve my real problem. I put this very simple example in order to avoid explaining some long code.
Then come forward with your "real problem".
As it is, while @novinosrin's code does work, it's extremely inefficient and a really stupid use of the macro facility.
No offense meant, @novinosrin!
05-18-2017 10:28 AM
Are you expecting 1) a separate value for the macro variable for each record in the input at the termination of the data set?
2) Or are you expecting the value to be an accumulation of the values?
1) will require creating multiple macro variables to reference
2) will require a more complete example of the actual issue at hand.
Note that in general referencing the value of macro variable created in the same data step it is created is extremely problematic.
Lines like this:
Var3 = Var2 + ¯ovar. ;
sort of have to have a value for the macro variable to compile before any line of code is actually executed.
You should have posted the ERROR your code generates and what you expect to actually do.
05-18-2017 10:40 AM
You can use the SYMGET() or SYMGETN() to retrieve the current value of macro variable during program execution.
data want; set have; call symputx('macrovar',Var1); Var3 = Var2 + symgetn('macrovar') ; run;