BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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...
2 REPLIES 2
deleted_user
Not applicable
Try &&field&n..

&& "protects" & against expanding (&& is converted into &)
SushilNayak
Obsidian | Level 7
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 🙂

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 744 views
  • 0 likes
  • 2 in conversation