BookmarkSubscribeRSS Feed
superman1
Fluorite | Level 6

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;
3 REPLIES 3
Tom
Super User Tom
Super User

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;
joseenrique1
SAS Employee

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;

Captura1.PNGCaptura2.PNG

I hope that this is helpful.

 

 

 

ballardw
Super User

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.

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!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 3 replies
  • 575 views
  • 0 likes
  • 4 in conversation