Help using Base SAS procedures

Rename with array statement....need some help

Accepted Solution Solved
Reply
New Contributor
Posts: 4
Accepted Solution

Rename with array statement....need some help

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


Accepted Solutions
Solution
‎02-17-2012 05:01 PM
PROC Star
Posts: 7,363

Rename with array statement....need some help

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


All Replies
Solution
‎02-17-2012 05:01 PM
PROC Star
Posts: 7,363

Rename with array statement....need some help

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

Super Contributor
Posts: 1,636

Rename with array statement....need some help

%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

Valued Guide
Posts: 632

Rename with array statement....need some help

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;

New Contributor
Posts: 4

Rename with array statement....need some help

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

Super User
Posts: 9,676

Rename with array statement....need some help

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

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 5756 views
  • 3 likes
  • 5 in conversation