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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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
  • 628 views
  • 0 likes
  • 2 in conversation