BookmarkSubscribeRSS Feed
BogdanC
Fluorite | Level 6

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;

 

8 REPLIES 8
PeterClemmensen
Tourmaline | Level 20

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;
BogdanC
Fluorite | Level 6
Because it would not solve my real problem. I put this very simple example in order to avoid explaining some long code.
novinosrin
Tourmaline | Level 20

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

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

PeterClemmensen
Tourmaline | Level 20

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 🙂

Kurt_Bremser
Super User

@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!

ballardw
Super User

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.

Tom
Super User Tom
Super User

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;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 8 replies
  • 10094 views
  • 4 likes
  • 7 in conversation