DATA Step, Macro, Functions and more

Macro variables referncing

Reply
N/A
Posts: 0

Macro variables referncing

Dear Sas users!
Could you please help me with example below.

%let field1=text1;
%let field2=text2;
%let field3=text3;

How can i reference particular fieldN variable if i know its number as a variable. For example %let n=1;
Referencing as &field&n is miscorrect...
N/A
Posts: 0

Re: Macro variables referncing

Try &&field&n..

&& "protects" & against expanding (&& is converted into &)
Contributor
Posts: 31

Re: Macro variables referncing

Hey Sonny,
Jaroslav is right!!!. the && would solve your problem.

example::

%let fruit1=Apple;
%let fruit2=Orange;
%let fruit3=Banana;

%let n=2;
proc means data=fruits;
title "Sales for fruit: &&section&n";
where section="&&section&n";
var saleprice;
run;

After macro variable resolution, the preceding program becomes:

proc means data=fruits;
title "Sales for fruit: Orange";
where section="Orange";
var saleprice;
run;

There are certain rules that macro processor follows to resolve macro variable references that contain multiple ampersands
> Macro variable references are resolved from left to right.
> Two ampersands (&&) resolve to one ampersand (&).
> Multiple leading ampersands cause the macro processor to rescan the reference
until no more ampersands can be resolved.

You can take advantage of these rules to create new macro variable
references.

Hope that helps Smiley Happy
Ask a Question
Discussion stats
  • 2 replies
  • 135 views
  • 0 likes
  • 2 in conversation