Hello,
I would like to change some variables from numeric to character. I use array function, but I found some error messages in the log window. Please help.
data cost_demagrapy_2;
set cost_demagrapy;
array var {*} Sex Age Insurance race;
do i=1 to dim(var);
rename newvar=var(i);
var_=put(newvar(i),10.);
drop var(i);
output;
end;
run;
So your code has a major logic error that the SAS compiler cannot detect.
But it also have two syntax errors that it can detect.
If you want to use parentheses in a variable name you need set the option VALIDVARNAME to ANY and then use a name literal in your code.
rename newvar='var(i)'n;
If you want to reference an element in an array you need to first define the array. You never defined any NEWVAR as an array.
Perhaps you meant to do something like:
data cost_demagrapy_2;
set cost_demagrapy;
array OLD Sex Age Insurance race;
array NEW $10 _Sex _Age _Insurance _race;
do index=1 to dim(old);
new[index]=put(old[index],10.);
end;
rename _Sex=Sex _Age=Age _Insurance=Insurance _race=race;
drop Sex Age Insurance race index;
run;
So your code has a major logic error that the SAS compiler cannot detect.
But it also have two syntax errors that it can detect.
If you want to use parentheses in a variable name you need set the option VALIDVARNAME to ANY and then use a name literal in your code.
rename newvar='var(i)'n;
If you want to reference an element in an array you need to first define the array. You never defined any NEWVAR as an array.
Perhaps you meant to do something like:
data cost_demagrapy_2;
set cost_demagrapy;
array OLD Sex Age Insurance race;
array NEW $10 _Sex _Age _Insurance _race;
do index=1 to dim(old);
new[index]=put(old[index],10.);
end;
rename _Sex=Sex _Age=Age _Insurance=Insurance _race=race;
drop Sex Age Insurance race index;
run;
Are you really trying to rename variables to the value of a different variable????
Not in rename statement. Rename is not executable. The Rename statement (or data set option) requires you to specify the name as text. You cannot use the value of a variable on the rename statement.
The data step basically doesn't allow you to use functions to build variable names at all.
Your partial statement "some variables from numeric to character. I use array function" just will not work. Variables in an array must be all of the same type, either all numeric or all character.
You can get the name of a variable referenced in an array with the VNAME such as where Var[i] is a legal array reference:
Thevariablenameis = vname(var[i]);
As we very frequently ask: provide an example of the data in data step form and the desired result.
I don't think you can use rename in that fashion.
You also seem to be referencing an array newvar(i) but I don't see any such array declared.
@ybz12003 wrote:
I don't really need to rename the variables. My main purpose is to change numeric to chatater.
SAS doesn't allow conversion within the same variable name so you need new variable names.
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.