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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 2 replies
  • 725 views
  • 0 likes
  • 2 in conversation