BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
DmytroYermak
Lapis Lazuli | Level 10

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

 

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

@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

2 REPLIES 2
Patrick
Opal | Level 21

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

DmytroYermak
Lapis Lazuli | Level 10

Thank you for the code and clarifications!

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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