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

Hi,

Can someone please explain me why my %do %while  stops after first iteration while it shouldn't...

I created a macro to perform a rename for a selected list of variable.

This the table that I would like to change the variable names :

data list;

infile cards  dsd missover;

input var1 $4. var2 $4. var3 $4. ;

cards;

aaa bbb ccc;

run;

In order to do so I get the new variable's name ( values of variable col )  from 'test' dataset (it is important to notice the value of col have underscore and I do not want to remove those underscore) :

data test;

infile cards  dsd missover;

input orig $4. col $20.;

cards;

var1 over_tap

var2 under_top_more

var3 any_over_before

;

run;

So I would like in list dataset to rename  var1  to 'over_tap', var2  to 'under_top_more' and var3 to 'any_over_before'.

So my code is :

%macro rename (data=) ;

proc sql;

select distinct orig,compress(col) into :old,:new  separated by ' '

from test;

quit;

data new_&data. ; set &data.;

%let count=1;

%do %while(%scan(&old,&count,%str( )) ne %str());

     %let old_&count=%scan(&old,&count);

     %let new_&count=%scan(&new,&count);

     rename &&old_&count.=&&new_&count.;

   %let count=%eval(&count+1);

%end;

run;

%mend rename;

%rename(data=list)

Unfortunately, my macro does only perform  one iteration...There are no ERROR or WARNING  and the log says :

SYMBOLGEN:  Macro variable COUNT resolves to 2

MLOGIC(RENAME):  %DO %WHILE() condition is FALSE; loop will not iterate again.

MPRINT(RENAME):   run;

Any clue ?

Regards,

sasp

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

You are only selecting one value into the macro variable OLD.  If you want multiple values you need to include the SEPARATED BY clause for each macro variable.

select distinct

  orig

,compress(col)

into

  : old separated by ' '

,: new separated by ' '

from test

;

View solution in original post

6 REPLIES 6
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Sorry, not going to read that as really can't look at %&%&'s.  As an alternative to doing it that way, why not just generate the code directly from the data you have:

data _null_;

     set test end=last;

     if _n_=1 then call execute(cats('data want; set list (rename=(',orig,'=',col));

     else call execute(cats(' ',orig,'=',col));

     if last then call execute(')); run;');

run;

This will generate a datastep with the renames from your dataset.

saskapa
Quartz | Level 8

Hi RW9,

Thanks, yes it indeed a solution.

However, I am still wondering why my %do %while iterate only once.

saskap

Tom
Super User Tom
Super User

You are only selecting one value into the macro variable OLD.  If you want multiple values you need to include the SEPARATED BY clause for each macro variable.

select distinct

  orig

,compress(col)

into

  : old separated by ' '

,: new separated by ' '

from test

;

saskapa
Quartz | Level 8

Thanks Tom, it works fine  !

Tom
Super User Tom
Super User

Unless you have a lot of variables you should be able to do it with one macro variable and no need for macro logic.

select distinct

  catx('=',orig,compress(col))

into

  : rename separated by ' '

from test

;


... rename &rename ;


Loko
Barite | Level 11

Hello,

from your code the value of &old is var1. The sql should be corrected like this

proc sql;

select distinct orig,compress(col) into :old separated by ' ',:new  separated by ' '

from test;

quit;

in order for the &old to be var1 var2 var3

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 6 replies
  • 3310 views
  • 7 likes
  • 4 in conversation