DATA Step, Macro, Functions and more

Trouble resolving macro variable

Reply
N/A
Posts: 0

Trouble resolving macro variable

Any suggestions on how to get 'hs' to resolve correctly?
data test;
input a1_1 a2_1 a3_1;
cards;
1 2 3
3 4 5
;
run;

%macro symps;

data test2 ( keep = a1 );
set test;
array a1_(1) a1_1;

call symput( 'hs' , trim( left( _n_ ) ) );
a1 = a&hs._1; /*i want this to resolve to something like a1=a2_1 */
put _all_;
run;

%mend symps;

%symps;
proc print data = test2;
run;
Super Contributor
Super Contributor
Posts: 3,174

Re: Trouble resolving macro variable

Posted in reply to deleted_user
Best to explain in your words (and illustrate both INPUT and desired OUTPUT sides) what it is that you want to accomplish.

Scott Barry
SBBWorks, Inc.
N/A
Posts: 0

Re: Trouble resolving macro variable

Sure. I want to assign the value contained in a3_1 to a new variable. But i need to reference an array that has the form aN_(index), where N = 1, 2, ... 10. The 'N' is determined in a prior loop in the same data step. In this example, I want a1 = 1 where _n_=1, a1 = 4 when _n_=2.

Does that help? My example was an attempt to simplify my current program. When i run this code, I get the warning "...hs not resolved." and error: "data step component object failure". Message was edited by: AndyC
N/A
Posts: 0

Re: Trouble resolving macro variable

Posted in reply to deleted_user
The macro variable 'hs' will be resolved before the symput is executed, so this won't work.

You could accomplish the intended result (*) by putting a1_1 a2_1 a3_1 into a data step array (say MYARRAY), and then you'd have something like:

a1 = myarray(_n_);

(*) But I'm not convinced I understand what you are really trying to accomplish.
N/A
Posts: 0

Re: Trouble resolving macro variable

Posted in reply to deleted_user
%macro symps;

data test2 ( keep = a1 );
set test;
array myarray(3) a1_1 a2_1 a3_1;
a = myarray(_n_);

run;

%mend symps;
Thanks that works in my example. Hope it will work in the context of my larger program. Andy
Super Contributor
Super Contributor
Posts: 3,174

Re: Trouble resolving macro variable

Posted in reply to deleted_user
Suggest you exercise the DATA step - recommend adding the line:

PUTLOG _ALL_;

so you can see the SAS variables at any given point in the DATA step portion of the program.

Also, your SAS log will likely have some tell-tale diagnostic NOTE, WARNING or ERROR messages for you to review and possibly debug, for example - given your code, I expect you will have a note:

NOTE: VARIABLE A IS UNINITIALIZED

and you will see from the PUTLOG that the named variable does not have an assigned value -- because of your assignment line.

And given that you likely want an observation for each A1 value, you will need to code a DO / END loop to bump through the array, and issue an OUTPUT statement within the DO loop.

Scott Barry
SBBWorks, Inc.
Ask a Question
Discussion stats
  • 5 replies
  • 142 views
  • 0 likes
  • 2 in conversation