I am trying to run the follwing macro fro simple purpose to rename the variables. But somehow the macro variables do not get resolved, unable to understand why.Any help would be appreciated.
%macro renaming;
%do i=1 %to &vmcnt;
data V_&i._b;
set V_&i._b;
rename _LEAF_=&&V_&i.._B;*These are the bins of interest of the varialbe which are generateted in form of 1,2,3 and etc.;
rename P_&target=&&V_&i.._P;*This is the avg probablity of target in each bin of the variable;
run;
%end;
%mend;
%renaming;
Log is:-
MLOGIC(RENAMING): %DO loop index variable I is now 3; loop will iterate again.
MPRINT(RENAMING): data V_3_b;
MPRINT(RENAMING): set V_3_b;
22: LINE and COLUMN cannot be determined.
NOTE 242-205: NOSPOOL is on. Rerunning with OPTION SPOOL may allow recovery of the LINE and COLUMN where the error has occurred.
ERROR 22-322: Syntax error, expecting one of the following: -, :, =.
76: LINE and COLUMN cannot be determined.
NOTE: NOSPOOL is on. Rerunning with OPTION SPOOL may allow recovery of the LINE and COLUMN where the error has occurred.
ERROR 76-322: Syntax error, statement will be ignored.
MPRINT(RENAMING): rename _LEAF_=ltv_orig _B;
22: LINE and COLUMN cannot be determined.
NOTE 242-205: NOSPOOL is on. Rerunning with OPTION SPOOL may allow recovery of the LINE and COLUMN where the error has occurred.
ERROR 22-322: Syntax error, expecting one of the following: -, :, =.
76: LINE and COLUMN cannot be determined.
NOTE: NOSPOOL is on. Rerunning with OPTION SPOOL may allow recovery of the LINE and COLUMN where the error has occurred.
ERROR 76-322: Syntax error, statement will be ignored.
MPRINT(RENAMING): rename P_lgd_tgt=ltv_orig _P;
MPRINT(RENAMING): run;
WARNING: Variable P_lgd_tgt cannot be renamed to ltv_orig because ltv_orig already exists.
NOTE: The SAS System stopped processing this step because of errors.
WARNING: The data set WORK.V_3_B may be incomplete. When this step was stopped there were 0 observations and 5 variables.
WARNING: Data set WORK.V_3_B was not replaced because this step was stopped.
NOTE: DATA statement used (Total process time):
real time 0.01 seconds
cpu time 0.00 seconds
You are confusing the token parsing. You can add %unquote() around the variable names so that the parser knows not to treat it as multiple tokens.
Personally I usually generate a new macro variable and then reference that.
%let V_1=XXX ;
%let target=YYY ;
%let vmcnt=1 ;
options mprint;
%macro renaming;
%do i=1 %to &vmcnt;
data V_&i._b;
set V_&i._b;
rename _LEAF_=%unquote(&&V_&i.._B);
rename P_&target=%unquote(&&V_&i.._P);
run;
%end;
%mend;
%renaming;
%macro renaming;
%do i=1 %to &vmcnt;
%let name=&&V_&i ;
data V_&i._b;
set V_&i._b;
rename _LEAF_=&name._B;
rename P_&target=&name._P ;
run;
%end;
%mend;
%renaming;
The resolved syntax as shown in your log is incorrect here, there can't be a space between ltv_orig and _B
rename _LEAF_=ltv_orig _B;
Why would that be? Well, I doubt we can tell because we don't know what &&v_&i contain or is supposed to resolve as, but it whatever it is, it appears to end with a blank space.
Hi,
But the question is how to resolve it. I have tried to use compress function, but did not solve the issue.
thanks.
HOW do you create the value ltv_orig _b? If you are concatenating something in a data step you may be introducing an additional space depending on the method you have chosen. Show us that part of the code.
Below is the part through which explains how I create ltv_orig not with_B.
ltv_orig_B is later that I want a different variable( in the coding case _leaf_ to be reanme ltv_orig_B)
Hope I am clear in my communication.
Thanks for the help
proc contents data=step1 noprint out=work.varlist;
run;
data varcnt;
set varlist END=LAST;;
where type=1;
call symput('V_'||left(_n_),name);
IF LAST THEN CALL SYMPUT('vmcnt',_N_);/*to find the last obsrvation)*/
run;
Use call symputX to avoid storing trailing spaces into the macro variables.
Or just use PROC SQL.
proc sql noprint ;
select name into :V_1 - :V_9999999
from varlist where type =1
;
%let vmcnt=&sqlobs ;
quit;
Swordfish wrote:
Below is the part through which explains how I create ltv_orig not with_B.
ltv_orig_B is later that I want a different variable( in the coding case _leaf_ to be reanme ltv_orig_B)Hope I am clear in my communication.
Thanks for the help
proc contents data=step1 noprint out=work.varlist;
run;
data varcnt;
set varlist END=LAST;;
where type=1;
call symput('V_'||left(_n_),name);
IF LAST THEN CALL SYMPUT('vmcnt',_N_);/*to find the last obsrvation)*/
run;
But we now need to know what are the values in your variable "name" that is used in call symput, and you haven't informed us of this information.
If name ends in a blank character, then that is (part of) the problem, and I don't think Tom's code above eliminates the issue if the value of name ends with a blank space.
You might want to try call symputx instead of call symput, as that will trim trailing and leading blanks, and probably eliminate the problem.
You are confusing the token parsing. You can add %unquote() around the variable names so that the parser knows not to treat it as multiple tokens.
Personally I usually generate a new macro variable and then reference that.
%let V_1=XXX ;
%let target=YYY ;
%let vmcnt=1 ;
options mprint;
%macro renaming;
%do i=1 %to &vmcnt;
data V_&i._b;
set V_&i._b;
rename _LEAF_=%unquote(&&V_&i.._B);
rename P_&target=%unquote(&&V_&i.._P);
run;
%end;
%mend;
%renaming;
%macro renaming;
%do i=1 %to &vmcnt;
%let name=&&V_&i ;
data V_&i._b;
set V_&i._b;
rename _LEAF_=&name._B;
rename P_&target=&name._P ;
run;
%end;
%mend;
%renaming;
Prior posts have identified the problem, as well as the best way to correct it. If you are dead set on leaving the problem values in place until the last minute, you can correct the problem inside your %do loop by adding a statement:
%do i=1 %to &vmcnt;
%let v_&i = &&v_&i;
The %LET statement will ignore leading and trailing blanks to the right of the equal sign, removing the offending trailing blank.
Looks like Tom and I were thinking along similar lines ... his method of copying to a new macro variable instead will work equally well!
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.