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 advise in the following case. I have to refer to a variable depending on the value of macrovariable. Please see below.

 

But I receive ERROR in the log: 

 

X=ABC&TR.X;
----
WARNING: Apparent symbolic reference TR not resolved.

The code:

 

data test;

	E='3';
	call symputx('TR',E);
		
	ABC3X='Variant 3';
	ABC5X='Variant 5';

	X=ABC&TR.X;

	put &TR.;

run;

How can it be resolved?

 

Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

This would be one way to accomplish what you attempted inside a data step:

 

data test;
	E='3';
	ABC3X='Variant 3';
	ABC5X='Variant 5';
	X= vvaluex(cats('ABC',E,'X'));
run;

The Vvaluex function will take either a variable value or expression that returns a variable name and find the value to assign to the target variable.

 

 

As I mentioned in the prior question about creating a macro variable in data step it is seldom needed to attempt to use that value inside the same data step, and most attempts will fail due to compliation and order of creation of the variables.

View solution in original post

9 REPLIES 9
WarrenKuhfeld
Rhodochrosite | Level 12

First SAS compiles your DATA step (attempting to resolve the macro variable).  It did not succeed because you have not yet defined it.  Had it succeeded, SAS would have next run the DATA step.  It is only then that the macro is set to a value.  So you need to use SYMPUTX in one DATA step and first use the value in the next step.  Alternatively, base your code on arrays or some other way that does not require macros.

Astounding
PROC Star

This can't happen within a DATA step.  SAS has to be able to look through the DATA step statements, check for syntax errors, and set up storage space for every variable used within the DATA step ... before actually executing any of the code.  There's no way to set up storage space for ABC&TR.X at that point, because the value of &TR is unknown until the DATA step actually executes.

 

As a macro application, there is no problem:

 

%let E = 3;

%let TR = &e;

%let ABC3X = Variant 3;

%let ABC5X = Variant 5;

%let X = ABC&TR.X;

 

Or possibly (depending on the intended result):

 

%let X = &&ABC&TR.X;

ballardw
Super User

This would be one way to accomplish what you attempted inside a data step:

 

data test;
	E='3';
	ABC3X='Variant 3';
	ABC5X='Variant 5';
	X= vvaluex(cats('ABC',E,'X'));
run;

The Vvaluex function will take either a variable value or expression that returns a variable name and find the value to assign to the target variable.

 

 

As I mentioned in the prior question about creating a macro variable in data step it is seldom needed to attempt to use that value inside the same data step, and most attempts will fail due to compliation and order of creation of the variables.

DmytroYermak
Lapis Lazuli | Level 10

 

 

data test;

	EPOCH='TREATMENT 3';
	period_N=substr(EPOC,11,1);
	call symputx('TP',period_N);

	ABC3X='Variant 3';
	ABC5X='Variant 5';
X= vvaluex(cats('ABC',"&TP.",'X')); run;

 

WarrenKuhfeld
Rhodochrosite | Level 12

Same answer as before.  The macro is not set to a value at compile time.  It is set later at run time.

ballardw
Super User
Please explain the need for creating a macro variable and using it in the same data step. Please. If this is just a test of creating a macro variable then you have demonstrated that creating it and attempting to use it this way will not work. The macro variable would be available in another data step if needed. Though for the specific test you have chosen to demonstrate the use there would be no need for a macro variable at all.
DmytroYermak
Lapis Lazuli | Level 10
Thank you. It was my misunderstanding - it seems I have to look into macros much deeper now ). I accepted your solution and using it in one training task that I'm doing now.
mkeintz
PROC Star

The principal is this:  data step compilation (translate from sas statement to machine instructions) must precede data step execution (i.e. the implementation of machine instructions).

 

That means the SAS data step Compiler needs the value of macrovar TP in order to compile the X=vvalue ... statement.  But macrovar TP will not be assigned a value until the data step executes (i.e. when the call symputx statement is already compiled and executed).

 

However, your sample program doesn't even need macrovars.  Drop the call symputx statement and change

   X= vvaluex(cats('ABC',"&TP.",'X'));

to

   X= vvaluex(cats('ABC',N_period,'X'));
or better yet, to

   X= vvaluex(cats('ABC',substr(epoch,11,1),'X'));

 

 

 

to

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
DmytroYermak
Lapis Lazuli | Level 10

Actually,  this is functioning as well:

symget(" ")

Different matter is that it is not needed here:

data test;

	E='3';
	call symputx('TR',E);
		
	ABC3X='Variant 3';
	ABC5X='Variant 5';

	X=vvaluex(cats('ABC',symget("TR"),'X'));

run;

proc print data=test; run;

I thought that the symget would not work here at all. 

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
  • 9 replies
  • 1359 views
  • 2 likes
  • 5 in conversation