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
%let whole = twohalfs;
%let twohalfs = fourquarters;
%put atlast=&&&whole;
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?
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);
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.
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
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?
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!
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.