- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
546 set cost_demagrapy;
547 array var {*} Sex Age Insurance race;
548 do i=1 to dim(var);
549 rename newvar=var(i);
-
22
76
550 var_=put(newvar(i),10.);
------
68
ERROR 22-322: Syntax error, expecting one of the following: a name, -, :, ;.
ERROR 76-322: Syntax error, statement will be ignored.
ERROR 68-185: The function NEWVAR is unknown, or cannot be accessed.
551 drop var(i);
-
22
76
ERROR 22-322: Syntax error, expecting one of the following: a name, -, :, ;, _ALL_, _CHARACTER_,
_CHAR_, _NUMERIC_.
ERROR 76-322: Syntax error, statement will be ignored.
552 output;
553 end;
554 run;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
@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.