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

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
  • 5 replies
  • 14114 views
  • 4 likes
  • 5 in conversation