DATA Step, Macro, Functions and more

Logic of macros assigning inside datastep (cycle as an example)

Accepted Solution Solved
Reply
Frequent Contributor
Posts: 142
Accepted Solution

Logic of macros assigning inside datastep (cycle as an example)

Hi all,

 

Could you please explain the logic of the following program (or if do I understand it correctly).

data test;
	do i=1 to 10;
		call symputx('Y',i);
			Z=&Y.;
				output;
	end;
run;

proc print noobs; run;

In my understanding when the compiler meets &Y the first time it has to assign the first meaning of i, i.e. "1". Then Z takes a value of Y. Then the cycle will be repeated and Z value should change. But in the output I have Z=10. How could it happen? Does this mean that if macroprocessor views that i should be calculated in a cycle it calculates it?

 

1.jpg

 


Accepted Solutions
Solution
‎09-24-2017 05:51 AM
Respected Advisor
Posts: 4,173

Re: Logic of macros assigning inside datastep (cycle as an example)

[ Edited ]
Posted in reply to DmytroYermak

@DmytroYermak

All macro stuff is pre-processing. Macro variable &Y resolves before your data step iterates and though has during iteration a constant value.

 

For your code to work macro variable &Y must have existed before you've run the data step (i.e. created in the same session where you've been implementing code).

If you close your session and run the code you've posted out of a brand new session, you'll get an error (because macro variable &Y doesn't exist prior to the data step executing).

 

Now for your example: Use SYMGET() to only retrieve the current value of macro variable &Y during the iteration of the data step.

data test;
  do i=1 to 10;
    call symputx('Y',i);
    z=symget("y");
/*    Z=&Y.;*/
    output;
  end;
run;

proc print noobs;
run;

 

N.B: You shouldn''t use a symput()/symget() combination in order to save away values for use in a later iteration of the data step. Use LAG() or RETAINed SAS variables instead.

When using symput() to create and populate a macro variable for later use in another run group: Code in a way that you call the function only as often as required. Calling the macro processor during a SAS data step impacts negatively on performance so it's something you should only do for good reason.

View solution in original post


All Replies
Solution
‎09-24-2017 05:51 AM
Respected Advisor
Posts: 4,173

Re: Logic of macros assigning inside datastep (cycle as an example)

[ Edited ]
Posted in reply to DmytroYermak

@DmytroYermak

All macro stuff is pre-processing. Macro variable &Y resolves before your data step iterates and though has during iteration a constant value.

 

For your code to work macro variable &Y must have existed before you've run the data step (i.e. created in the same session where you've been implementing code).

If you close your session and run the code you've posted out of a brand new session, you'll get an error (because macro variable &Y doesn't exist prior to the data step executing).

 

Now for your example: Use SYMGET() to only retrieve the current value of macro variable &Y during the iteration of the data step.

data test;
  do i=1 to 10;
    call symputx('Y',i);
    z=symget("y");
/*    Z=&Y.;*/
    output;
  end;
run;

proc print noobs;
run;

 

N.B: You shouldn''t use a symput()/symget() combination in order to save away values for use in a later iteration of the data step. Use LAG() or RETAINed SAS variables instead.

When using symput() to create and populate a macro variable for later use in another run group: Code in a way that you call the function only as often as required. Calling the macro processor during a SAS data step impacts negatively on performance so it's something you should only do for good reason.

Frequent Contributor
Posts: 142

Re: Logic of macros assigning inside datastep (cycle as an example)

Thank you for the code and clarifications!

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 144 views
  • 0 likes
  • 2 in conversation