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

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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