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: Call for Content

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 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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