BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
ybz12003
Rhodochrosite | Level 12

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;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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;

 

View solution in original post

5 REPLIES 5
PaigeMiller
Diamond | Level 26

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.

PaigeMiller_0-1663012019648.png

 

All of this has been requested of you before. We are trying to help you, but you have to help us.

 

--
Paige Miller
Tom
Super User Tom
Super User

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;

 

ybz12003
Rhodochrosite | Level 12
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;
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

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
  • 450 views
  • 1 like
  • 4 in conversation