## How to assign the value of a column to a macro variable in a data step, on each iteration?

Occasional Contributor
Posts: 9

# How to assign the value of a column to a macro variable in a data step, on each iteration?

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 + &macrovar. ; /*equivalent of Var3 = Var2 + Var1*/
run;

PROC Star
Posts: 1,283

## Re: How to assign the value of a column to a macro variable in a data step, on each iteration?

[ Edited ]

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;
Occasional Contributor
Posts: 9

## Re: How to assign the value of a column to a macro variable in a data step, on each iteration?

Because it would not solve my real problem. I put this very simple example in order to avoid explaining some long code.
PROC Star
Posts: 1,817

## Re: How to assign the value of a column to a macro variable in a data step, on each iteration?

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('&macrovar') ; /*equivalent of Var3 = Var2 + Var1*/

run;

Regards,

Naveen Srinivasan

Super User
Posts: 9,599

## Re: How to assign the value of a column to a macro variable in a data step, on each iteration?

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.

PROC Star
Posts: 1,283

## Re: How to assign the value of a column to a macro variable in a data step, on each iteration?

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

Super User
Posts: 10,270

## Re: How to assign the value of a column to a macro variable in a data step, on each iteration?

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!

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Posts: 13,563

## Re: How to assign the value of a column to a macro variable in a data step, on each iteration?

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 + &macrovar. ;

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.

Super User
Posts: 8,115

## Re: How to assign the value of a column to a macro variable in a data step, on each iteration?

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;
Discussion stats
• 8 replies
• 766 views
• 1 like
• 7 in conversation