BookmarkSubscribeRSS Feed
rstarin
Calcite | Level 5

How do I reference a variable via its string name?  Here's an example of what I mean.  Let's say I had the following variables:

whole = "twohalfs";

twohalfs = "fourquarters";

and I wanted to have a macro whose input was whole, but could access the contents of twohalfs. I formatting aside, I would expect something like this to work:

%macro dostuff(whole);

output = &&whole;

    

put output;

%mend dostuff;

However, when I run something like that, &whole resolves to "twohalfs".  But then there's no second resolution.  And I'm just left outputting twohalfs and not fourquarters.  But I want fourquartersI suspect that there's a simple way to do this.  Please let me know your thoughts, or if you have any further questions.

Thank you very much,

Robert Starin

7 REPLIES 7
Haikuo
Onyx | Level 15

%let whole = twohalfs;

%let twohalfs = fourquarters;

%put atlast=&&&whole;

rstarin
Calcite | Level 5

The thing that isn't working for me is when I do it with the quotes.  I don't know how to use the string "twohalfs" and get the contents of the variable called twohalfs.

Do you know how to do that?

Haikuo
Onyx | Level 15

Not sure if I have completely understand your need, but:

%let whole = "&twohalfs";

%let twohalfs = fourquarters;

%put atlast=&whole;

update:

Or in a step by step lagging mode:

%let whole = %nrstr("&twohalfs");

%let twohalfs = fourquarters;

%put atlast=%unquote(&whole);

data_null__
Jade | Level 19

I think this may be what you're trying to do. You need to move the quotes to the place where they are needed.  If this is not what you want explain your goal better.

32         %let whole = twohalfs;
33         %let twohalfs = fourquarters;
34        
35        
36         %macro dostuff(whole);
37            output = "&&&whole";
38            put output=;
39            %mend dostuff;
40        
41         options mprint=1;
42         data _null_;
43            %dostuff(twohalfs);
MPRINT(DOSTUFF):   output = "fourquarters";
MPRINT(DOSTUFF):   put output=;
44            run;

output=fourquarters
rstarin
Calcite | Level 5

Sorry, I think I have done a poor job of explaining what I'm doing.  I am looking to have a macro with an unknown number of parameters, and then within the macro, create new variables based on the names of those parameters and the value of the variables that they reference.

I figured that to do this, I should bring the parameters in via a string list or an array, and then read them out to create and set the new variable.

I am able to hardcode this functionality for one (or a known set of parameters)with something like this:

%macro dostuff(a,b);

&a._temp = &a;

&b._temp = &b;

%mend dostuff;

data _null_;

ONE = 1;

TWO = 2;

%dostuff(ONE,TWO);

run;

would yield ONEtemp = 1 and TWOtemp = 2.  However, when I pass with an array (like parts{*} ONE TWO), I am having trouble creating the new variable name, and when I pass with a string list (like "ONE,TWO"), I am having trouble referencing the value of the named variable.  So this question was originally in regards to how to do this with a string list -- but the array route (or any other) is fine with me.

Thanks very much for your help!

-Bob

Astounding
PROC Star

Suppose you were to create this sort of macro:

%dostuff (varlist=ONE TWO THREE FOUR)

You could certainly use the parameter inside a DATA step:

array parts {*} &varlist;

Would it be helpful to learn how macro language could set up a %DO loop to process ONE, then TWO, then THREE, then FOUR?

data_null__
Jade | Level 19

Have a look at the documentation for the %MACRO statement PBUF or PARMBUF option or something like that.    However like says its easier to use blank delimited variable list.  It's more like the way SAS works.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 7 replies
  • 961 views
  • 0 likes
  • 4 in conversation