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!
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.
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.
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;
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.
Thank you, I used the 'vvalue'! The question is why the next code is not functioning and is it possible to manage it.
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;
Same answer as before. The macro is not set to a value at compile time. It is set later at run time.
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
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: