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;
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;
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.
data have;
input Var1 Var2;
datalines;
4 10
7 20
9 30
3 40
2 50
5 60
;
data want;
set have;
call symputx('macrovar',Var1);
Var3 = Var2 + resolve('¯ovar') ; /*equivalent of Var3 = Var2 + Var1*/
run;
Regards,
Naveen Srinivasan
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.
Though, @novinosrin's soultion is nice and solves your examplified problem, I agree with @RW9. If you explained your real problem you would most likely find that there is a simpler (preferably non macro-based) solution to your problem and you might even learn something 🙂
@BogdanC wrote:
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!
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.
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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.