Hello,
I would like to rename several variables in the sample dataset. An error message was shown in the log window. Please let me know how to resolve them.
data want;
set have;
array OldName (i) Room_ED Room_ICU ;
array NewName (i) Room_ED_ Room_ICU_ ;
do i = 1 to dim(OldName);
rename NewName(i)= OldName(i);
end;
run;
53 do i = 1 to dim(OldName);
54 rename NewName(i)= OldName(i);
-
22
76
ERROR 22-322: Syntax error, expecting one of the following: -, :, =.
ERROR 76-322: Syntax error, statement will be ignored.
55 end;
56 run;
The RENAME statement needs the actual names of variables. Not an expression.
You could use your DO loop to copy the VALUES from one array to the other. You could then drop the old variables and keep the new ones.
So if the old variables are numeric your array statements could work like this:
data want;
set have;
array OldName Room_ED Room_ICU ;
array NewName Room_ED_ Room_ICU_ ;
do i = 1 to dim(OldName);
NewName[i]= OldName[i];
end;
drop i Room_ED Room_ICU ;
run;
If the variables are character then you need to add a length to the ARRAY statement. So if you want the two new variables to be character strings of length 10 the array statement would be:
array NewName $10 Room_ED_ Room_ICU_ ;
If you have the list of variable names as data then you could use that to GENERATE a rename statement.
For example by writing it to a file.
data names;
input name :$32. ;
cards;
Room_ED
Room_ICU
;
filename code temp;
data _null_;
set names end=eof;
file code;
if _n_=1 then put 'rename ' @;
put name '=' name +(-1) '_' @;
if eof then put ';' ;
run;
You could then tell SAS to use that file as code using the %INCLUDE statement.
Either in a data step.
data want;
set have;
%include code / source2;
run;
Or by modifying the original dataset using PROC DATASETS.
proc datasets nolist lib=work;
modify have;
%include code / source2;
run;
quit;
Please show us the ENTIRE log for the DATA step that has an error. Do NOT choose parts of the log to show us and choose other parts of the log to not show us.
Please use the </> icon to show us the log so we can view it with proper spacing.
All of this has been requested of you before. We are trying to help you, but you have to help us.
The RENAME statement needs the actual names of variables. Not an expression.
You could use your DO loop to copy the VALUES from one array to the other. You could then drop the old variables and keep the new ones.
So if the old variables are numeric your array statements could work like this:
data want;
set have;
array OldName Room_ED Room_ICU ;
array NewName Room_ED_ Room_ICU_ ;
do i = 1 to dim(OldName);
NewName[i]= OldName[i];
end;
drop i Room_ED Room_ICU ;
run;
If the variables are character then you need to add a length to the ARRAY statement. So if you want the two new variables to be character strings of length 10 the array statement would be:
array NewName $10 Room_ED_ Room_ICU_ ;
If you have the list of variable names as data then you could use that to GENERATE a rename statement.
For example by writing it to a file.
data names;
input name :$32. ;
cards;
Room_ED
Room_ICU
;
filename code temp;
data _null_;
set names end=eof;
file code;
if _n_=1 then put 'rename ' @;
put name '=' name +(-1) '_' @;
if eof then put ';' ;
run;
You could then tell SAS to use that file as code using the %INCLUDE statement.
Either in a data step.
data want;
set have;
%include code / source2;
run;
Or by modifying the original dataset using PROC DATASETS.
proc datasets nolist lib=work;
modify have;
%include code / source2;
run;
quit;
53 do i = 1 to dim(OldName); 54 rename NewName(i)= OldName(i); - 22 76 ERROR 22-322: Syntax error, expecting one of the following: -, :, =. ERROR 76-322: Syntax error, statement will be ignored. 55 end;
Hello, @ybz12003 I see you already have an answer to your question, but in the future, we need the ENTIRE log for the DATA step, not the couple of lines that you choose to show us.
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 how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.