DATA Step, Macro, Functions and more

Evaluating a macro variable string that contains variable names

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 16
Accepted Solution

Evaluating a macro variable string that contains variable names

Hi,

 

I would be very grateful if someone could please help me with the following query.  I have a macro variable that contains a string, but the expression contains variable names.  In the example below, the macro variable contains an expression that refers to variables 'a' and 'b'.  The dataset 'testdata' contains these variables.  I would like to know how to set 'c' to be the numeric result of evaluating the macro variable 'test', so should equal 3.

 

%Let test = "a+b";

 

 

Data testdata;

   a=1;

   b=2;

       c=???;

Run;

 

Many thanks,

 

Hoa


Accepted Solutions
Solution
‎03-05-2017 12:57 PM
Occasional Contributor
Posts: 8

Re: Evaluating a macro variable string that contains variable names

%Let test = a+b;

Data testdata;
	a=1;
	b=2;
	c=&test.;
Run;

View solution in original post


All Replies
Super User
Posts: 17,828

Re: Evaluating a macro variable string that contains variable names

Remove quotes from the %let statement. 

 

Replace ??? In datastep with &c. 

 

At its basics consider macro variables as a Find/Replace algorithm. The result needs to be valid SAS code. 

Solution
‎03-05-2017 12:57 PM
Occasional Contributor
Posts: 8

Re: Evaluating a macro variable string that contains variable names

%Let test = a+b;

Data testdata;
	a=1;
	b=2;
	c=&test.;
Run;
Occasional Contributor
Posts: 16

Re: Evaluating a macro variable string that contains variable names

Thank you very much! I forgot I had the quotes in the macro expression - too much staring at code..! 😃
☑ This topic is SOLVED.

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

Discussion stats
  • 3 replies
  • 208 views
  • 1 like
  • 3 in conversation