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;
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.
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.
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().
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.
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;
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!
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
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;
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
@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).
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!
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.