DATA Step, Macro, Functions and more

How to refer to a variable depending on the value of macrovariable

Accepted Solution Solved
Reply
Regular Contributor
Posts: 210
Accepted Solution

How to refer to a variable depending on the value of macrovariable

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!


Accepted Solutions
Solution
‎09-20-2017 04:25 PM
Super User
Posts: 13,066

Re: How to refer to a variable depending on the value of macrovariable

Posted in reply to DmytroYermak

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


All Replies
SAS Super FREQ
Posts: 496

Re: How to refer to a variable depending on the value of macrovariable

Posted in reply to DmytroYermak

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.

Super User
Posts: 6,542

Re: How to refer to a variable depending on the value of macrovariable

[ Edited ]
Posted in reply to DmytroYermak

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;

Solution
‎09-20-2017 04:25 PM
Super User
Posts: 13,066

Re: How to refer to a variable depending on the value of macrovariable

Posted in reply to DmytroYermak

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.

Regular Contributor
Posts: 210

Re: How to refer to a variable depending on the value of macrovariable

 

 

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;

 

SAS Super FREQ
Posts: 496

Re: How to refer to a variable depending on the value of macrovariable

Posted in reply to DmytroYermak

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

Super User
Posts: 13,066

Re: How to refer to a variable depending on the value of macrovariable

Posted in reply to DmytroYermak
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.
Regular Contributor
Posts: 210

Re: How to refer to a variable depending on the value of macrovariable

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.
Trusted Advisor
Posts: 1,288

Re: How to refer to a variable depending on the value of macrovariable

Posted in reply to DmytroYermak

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

Regular Contributor
Posts: 210

Re: How to refer to a variable depending on the value of macrovariable

[ Edited ]

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. 

☑ This topic is solved.

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

Discussion stats
  • 9 replies
  • 257 views
  • 2 likes
  • 5 in conversation