- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hello
I'd like to try and use an array to change my character variables to numeric variables. Arrays are confusing to me since I am new and much help would be appreciated.
ill give an example of a code I am trying but im not sure if I am even starting correctly?
I have 12 variables that I want to change from char to numeric.
Array practice[12] $ firstvar-lastvar;
do I=1 to 12;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Before you can start trying to apply the same operation to multiple variables by using arrays you need to first know what operation you want perform.
You cannot change the type of a variable, so to convert a character variable to a numeric variable you will need to make a new variable.
newvar = input(oldvar,32.);
So in order to convert multiple variables you could just replicate that statement, change the variable names.
newvar1 = input(oldvar1,32.);
newvar2 = input(oldvar2,32.);
newvar3 = input(oldvar3,32.);
If you want to try instead to use array then you will need two arrays. One for the original character variable and another for the new numeric variables.
array old oldvar1 oldvar2 oldvar3;
array new newvar1 newvar2 newvar3;
Now you can make a DO loop to index through the arrays and make the conversion.
do index=1 to dim(old);
new[index] = input(old[index],32.);
end;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
If you want to change numbers in character variables to numbers in numeric variables, it could be necessary to use the input funcion. Here you have an example:
data work.test;
x1='101,001.23';
x2='1,001.00';
x3='1.23';
run;
data work.test2(keep=y);
set work.test;
array a[3] $x1-x3;
do i = 1 to 3;
y = input(a[i],comma10.2); output;
end;
run;
I hope that this is helpful.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
One question is why are variables that you apparently expect to be numeric actually character?
A very common reason for this is using either Proc Import or a wizard that is guessing about the content of your data and the actual data has one or more bits appearing that are causing issues? Does this sound familiar/ likely?
Fixing by learning to read data properly can save a lot of time "fixing" things later.