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
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
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
%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
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;
Thanks to both of you. %scan works. Art, thanks for the additional explination, very helpful.
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
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.