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

Hello everyone! I'm new to the forums so I'm sorry if this is in the wrong place to post this question. Basically I'm completely stumped here. I got the follwoing code, I'm hoping to use to rename elements of an array....

   

data notarget_temp1;

set &dset.;

array arr_varlst{*} &varnames.;

%do i = 1 %to &nvars.;

rename arr_varlst{&i.} = key_&i.;

%end;

run;

The problem appears to the rename statement, as I get the following error...

ERROR 22-322: Syntax error, expecting one of the following: -, :, =.

Really I have no idea what is going on here. Pretty sure I've used code exactly like this in the past. As an FYI the whole thing is wrapped in a bigger macro, all the macro variables in the block are resolving fine...

Thanks,

Joe

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

I don't think you need an array.  How about?:

%let dset=sashelp.class;

%let varnames=age height weight;

%let nvars=3;

%macro doit;

  data notarget_temp1;

    set &dset.;

    %do i = 1 %to &nvars.;

      rename %scan(&varnames.,&i.) = key_&i.;

   %end;

  run;

%mend;

%doit

View solution in original post

5 REPLIES 5
art297
Opal | Level 21

I don't think you need an array.  How about?:

%let dset=sashelp.class;

%let varnames=age height weight;

%let nvars=3;

%macro doit;

  data notarget_temp1;

    set &dset.;

    %do i = 1 %to &nvars.;

      rename %scan(&varnames.,&i.) = key_&i.;

   %end;

  run;

%mend;

%doit

Linlin
Lapis Lazuli | Level 10

%let dset=sashelp.class;

%let varnames=age height weight;

%let nvars=3;

%macro doit;

  data notarget_temp1;

    set &dset.;

      rename %do i = 1 %to &nvars.;

                  %scan(&varnames.,&i.) = key_&i.

                  %end;;

  run;

%mend;

%doit

ArtC
Rhodochrosite | Level 12

Macro processing is completed before the DATA step executes.  This means that array  will not yet exist when the %DO executes.  You need to extract the variable name without using the array.  The %SCAN macro function will do the trick.  Untested code:

data notarget_temp1;

set &dset.;

%do i = 1 %to &nvars.;

rename %scan(&varnames,&i,%str( )) = key_&i.;

%end;

run;

jcauteru
Calcite | Level 5

Thanks to both of you. %scan works. Art, thanks for the additional explination, very helpful.

Ksharp
Super User

Or use proc datasets which could be faster.

data class; 
 set sashelp.class;
run;


%let libname=work;
%let dset=class;
%let varnames=age height weight;

%macro doit;
proc datasets library=&libname memtype=data nolist;
 modify &dset ;
 rename 
 %do i=1 %to %sysfunc(countw(&varnames));
  %scan(&varnames,&i)= key&i
 %end;
 ;
quit;
%mend doit;

%doit
  


Ksharp

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 15334 views
  • 4 likes
  • 5 in conversation