BookmarkSubscribeRSS Feed
Josh123
Calcite | Level 5

Hello Everyone and thanks for your help!

 

I have a macro producing a number of tables through a loop, but the value of &variable defined in the symputx command is not carrying through to the rest of the macro. Any suggestions?

 

proc sql NOPRINT;

/*select name into :varlist separated by ' '*/

create table _Null_ as select * from temp;

quit;

%let m=&sqlobs;

%put &m;

 

%macro tablecreate (Variable);

%do i=1 %to &m;

%put &i;

 

data tempmod;

set temp;

where seq=&i;

call symputx('Variable'||trim(left(_n_)), Test);

 

run;

 

data resultsmerged2; set resultsmerged2;

retain base_val;

if timeptno=1 then

 

do;

base_val = &variable;

end;

change_bl = &variable-base_val;

run;

 

.

.

.

 

%end;

 

 

%mend tablecreate;

 

16 REPLIES 16
Tom
Super User Tom
Super User

Which macro variable are you asking about?

The macro definition defines VARIABLE as a parameter. So there is a macro variable named VARIABLE.  But I don't see any attempt to change the value of it over what it got when the macro was called.

 

The CALL SYMPUTX() function call is creating a  series of macro variables named VARIABLE1 to VARIABLE<n> where N is the number of observations in TEMP that have the variable SEQ equal to your loop counter &I.

Josh123
Calcite | Level 5

The variable is the "Variable" created from the symputx. I just need it to carry through to the rest of the macro. The table creation and reporting is done for a metric (ex: HR) and then moves on to the next metric, with a list imported through a csv file. I guess the real question is after the symputx, how do I call that variable in the next data step.

Reeza
Super User
You need to add a parameter scope to the CALL SYMPUTX(). You're creating the macro variable, but only within the macro and hten outside of the macro it doesn't exist. This is a concept known as 'scope' of a variable.

CALL SYMPUTX() has a third parameter that allows you to set the variable as global or local, you want to set it to global.

call symputx(..., ...., 'g')

Everyone else's comments are also valid though.
Kurt_Bremser
Super User

You are creating the variable in a macro without defining it global, so it will be local to the macro and vanish as soon as the macro stops executing.

Add "g" as third parameter to the call symputx().

Josh123
Calcite | Level 5

Thank you for your reply. I'm trying to use &variable in the next step inside the macro, but it's not recognizing the variable as populated.

Reeza
Super User
Have you tried the solutions posted, changing the scope in your CALL SYMPUTX()? That should solve your problem. If, not please post the code you ran and the log that indicate the errors.
Kurt_Bremser
Super User

I see no use of macro variable &variable1. or similar in this macro code:

%macro tablecreate (Variable);
%do i=1 %to &m;
  %put &i;

data tempmod;
set temp;
where seq=&i;
call symputx('Variable'||trim(left(_n_)), Test);
run;

data resultsmerged2; set resultsmerged2;
retain base_val;
if timeptno=1 then do;
  base_val = &variable;
end;
change_bl = &variable-base_val;
run;

.

.

.

%end;
%mend tablecreate;
Josh123
Calcite | Level 5

With both updates:

 

proc sql NOPRINT;

/*select name into :varlist separated by ' '*/

create table _Null_ as select * from temp;

quit;

%let m=&sqlobs;

%put &m;

%macro tablecreate (Variable);

%do i=1 %to &m;

%put &i;

 

data tempmod;

set temp;

where seq=&i;

call symputx('Variable'||trim(left(_n_)), Test, 'g');

 

run;

 

data resultsmerged2; set resultsmerged2;

retain base_val;

if timeptno=1 then

 

do;

base_val = &variable&i;

end;

change_bl = &variable&i-base_val;

run;

 

.

.

.

 

%end;

%mend tablecreate;

 

%tablecreate;

 

Log doesn't show any errors but all records are now producing a value of 1 for base_val and 0 for change_bl. I'm try to take the variable name from the import and use it as the field name in dataset resultsmerged2. Any other ideas?

 

Thanks!

 

Reeza
Super User
So the original problem is solved and you now have further errors. This is kind of a painful back and forth here. Can you maybe start at the beginning, explain what you're trying to solve and we can offer some suggestions? I feel like this is an XY problem.

>The XY problem is a communication problem encountered in help desk and similar situations in which the real issue, X, of the person asking for help is obscured, because instead of asking directly about issue X, they ask how to solve a secondary issue, Y, which they believe will allow them to resolve issue X. However, resolving issue Y often does not resolve issue X, or is a poor way to resolve it, and the obscuring of the real issue and the introduction of the potentially strange secondary issue can lead to the person trying to help having unnecessary difficulties in communication and offering poor solutions.
Josh123
Calcite | Level 5

Fair enough:

 

I'm importing a dataset that translates a text string (a) to another text string (b). I need to then take (b) and use it as a field reference in another dataset. I'm looping through the initial dataset of a to b conversions and for each (b) taking metrics from the field in dataset resultsmerged2. So for example, the first (b) is HR, so I need to add HR to &variable and use it in calculations in the calculation for baseline and change from baseline. Everything works until I try to use the variable. The data tempmod needs to define &variable somehow so I can use it in other sections of the same macro. I can try to explain anything else that is unclear

 

Thanks,

Josh 

Tom
Super User Tom
Super User

Most of it is unclear. 

What is clear is that you have not understood the early suggestions that you need to reference the macro variable that you actually created.

 

For example this CALL SYMPUTX() function call is creating a macro variable name MYVARNAME with the value HR. Then the PROC MEANS step is using that macro variable in the VAR statement so that the variable named HR in the dataset named MYDATA is what is being analyzed.

data _null_;
  call symputx('MYVARNAME','HR');
run;
proc means data=mydata;
  var &myvarname;
run;
Josh123
Calcite | Level 5

I appreciate the response. While I may not have understood, the issue is directly between the variable declaration and usage in the following data step. Rather than try to reinvent the wheel here, I am looking for a solution to how to call the previous variable defined as &variable. None of the suggestions thus far have worked, so still trying to find a solution.

 

Thanks,

Josh

Reeza
Super User
You need to resolve it as &&Variable&i.
Tom
Super User Tom
Super User

@Josh123 wrote:

I appreciate the response. While I may not have understood, the issue is directly between the variable declaration and usage in the following data step. Rather than try to reinvent the wheel here, I am looking for a solution to how to call the previous variable defined as &variable. None of the suggestions thus far have worked, so still trying to find a solution.

 

Thanks,

Josh


You did NOT create a macro variable named VARIABLE.

You created a variable named VARIABLE1 (and possibly some other macro variables, we cannot tell with out data or at least SAS log).

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 16 replies
  • 2216 views
  • 2 likes
  • 4 in conversation