BookmarkSubscribeRSS Feed
Aditi24
Obsidian | Level 7

Hi,

 

I have a macro variable :-

 

%let data1= cars;

%let data2= air;

%let data3= demo;

 

and I am using these macro in a do loop:-

 

%do i %to 3

proc print data=test1.data&i;
run;

%end;

 

But its not resolving to the desired variable. How to resolve this half macro half string macro variable.

Please Help.

4 REPLIES 4
LinusH
Tourmaline | Level 20

So you want for &i = 1 that data set resolve to cars?

You current logic resolves only &I as per you code. You need to apply a & if you want to use a name as a reference to macro variable name, and for resolution: &data1.

Since this is a two step resolution you need to add additional & so the resolution takes place in the order you need.

Data never sleeps
Aditi24
Obsidian | Level 7

Could you let me know how to doubly resolve this? @LinusH

Kurt_Bremser
Super User

For indirect macro variable references, you need to use a double ampersand:

%let data1= cars;
%let data2= class;
%let data3= heart;

%macro loop;
%do i = 1 %to 3;

proc print data=sashelp.&&data&i;
run;

%end;
%mend;
%loop

Use the "little running man" icon to post code, as it will preserve formatting of the code (no additional linefeeds etc when copy/pasting to SAS)

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
  • 4 replies
  • 779 views
  • 3 likes
  • 3 in conversation