DATA Step, Macro, Functions and more

Question about referencing a variable through its name as a string

Reply
New Contributor
Posts: 3

Question about referencing a variable through its name as a string

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

Respected Advisor
Posts: 3,124

Re: Question about referencing a variable through its name as a string

%let whole = twohalfs;

%let twohalfs = fourquarters;

%put atlast=&&&whole;

New Contributor
Posts: 3

Re: Question about referencing a variable through its name as a string

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?

Respected Advisor
Posts: 3,124

Re: Question about referencing a variable through its name as a string

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);

Respected Advisor
Posts: 3,777

Re: Question about referencing a variable through its name as a string

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
New Contributor
Posts: 3

Re: Question about referencing a variable through its name as a string

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

Super User
Posts: 5,082

Re: Question about referencing a variable through its name as a string

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?

Respected Advisor
Posts: 3,777

Re: Question about referencing a variable through its name as a string

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.

Ask a Question
Discussion stats
  • 7 replies
  • 228 views
  • 0 likes
  • 4 in conversation