BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Swordfish
Calcite | Level 5

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

                                 

                           

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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;

View solution in original post

8 REPLIES 8
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
Swordfish
Calcite | Level 5

Hi,

But the question is how to resolve it. I have tried to use compress function, but did not solve the issue.

thanks.

ballardw
Super User

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.

Swordfish
Calcite | Level 5

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;

Tom
Super User Tom
Super User

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;

PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
Tom
Super User Tom
Super User

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;

Astounding
PROC Star

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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 8 replies
  • 13755 views
  • 6 likes
  • 5 in conversation