I have some mixed columns in an Excel data. So I read all variables into

SAS as character variables. Now I want to transfer all character variables

to numerical variables, but how should I write the programI wrote the

following program, but it doesn't work. Can anybody tell me where is wrong

or how to write correctly

data chen;

set chen;

array XXX[*] _character_;

do kk=1 to DIM(XXX);

XXX[kk]=input(XXX[kk],1.);

end;

run;

Thanks~~

Hi Chiu-chen,

If the variables number in your dataset is not too many, you can try

the following method:

data temp;

input id $ x $ y $;

cards;

1001 2 3

1002 1 5

1003 1 1

;

run;

proc sql noprint;

create table temp2 as

select id,

input(x,8.) as x format=best12. informat=12. length=8,

input(y,8.) as y format=best12. informat=12. length=8 from temp;

quit;

Best regards.

Sean

If the variables number in your dataset is not too many, you can try

the following method:

data temp;

input id $ x $ y $;

cards;

1001 2 3

1002 1 5

1003 1 1

;

run;

proc sql noprint;

create table temp2 as

select id,

input(x,8.) as x format=best12. informat=12. length=8,

input(y,8.) as y format=best12. informat=12. length=8 from temp;

quit;

Best regards.

Sean

Would the following do what you want?

data chen;

input a $ b $ c $;

cards;

1 2 3

a 4 5

6 b 7

8 c d

;

run;

data chen;

set chen;

array XXX[*] _character_;

array YYY[3];

do kk=1 to DIM(XXX);

YYY[kk]=input(XXX[kk],1.);

end;

run;

Art

---------

On Sun, 24 Jul 2005 04:57:04 -0400, subscribe sas-L Chen, Chiu-Chen

data chen;

input a $ b $ c $;

cards;

1 2 3

a 4 5

6 b 7

8 c d

;

run;

data chen;

set chen;

array XXX[*] _character_;

array YYY[3];

do kk=1 to DIM(XXX);

YYY[kk]=input(XXX[kk],1.);

end;

run;

Art

---------

On Sun, 24 Jul 2005 04:57:04 -0400, subscribe sas-L Chen, Chiu-Chen

Chiu-Chen,

There is no way easy way to get SAS to do what you want. The problem will

coms in since you have some vars that are true character variables, some

that are true numerics held as characters, and here is the problem some that

are a mixture of character and numeric and right fully held as character.

If I understand you correctly you have all vars as character. Thus any mass

indescriminant conversion will give you errors in your log and not a good

error at that.

To do what you want all you need is a few simple things, first a list of the

numeric variables held as text that you want converted to numeric, second a

rename statement, length statement,, a drop statement on th eoutput side of

things, and lastly an assignment statement.

data one ;

infile cards dsd dlm = ' ' ;

input x1 $ x2 $ x3 $ x4 $ ;

cards ;

A 1 1 1

B 2 B 2

C 3 C 3

D 4 4 4

;

run ;

data two (drop = _:) ;

length x2 x4 8. ;

set one (rename = (x2 = _x2 x4 = _x4)) ;

x2 = _x2 ;

x4 = _x4 ;

run ;

proc contents ;

run ;

Now if you have a lot of vars you need converted this process can be made

easier by the use of either macros and/or arays.

Toby Dunn

From: "subscribe sas-L Chen, Chiu-Chen" < XXXX@XXXXX.COM >

Reply-To: "subscribe sas-L Chen, Chiu-Chen" < XXXX@XXXXX.COM >

To: XXXX@XXXXX.COM

Subject: How to transfer character variables into numeric variables by

using DO LOOP

Date: Sun, 24 Jul 2005 04:57:04 -0400

I have some mixed columns in an Excel data. So I read all variables into

SAS as character variables. Now I want to transfer all character variables

to numerical variables, but how should I write the programI wrote the

following program, but it doesn't work. Can anybody tell me where is wrong

or how to write correctly

data chen;

set chen;

array XXX[*] _character_;

do kk=1 to DIM(XXX);

XXX[kk]=input(XXX[kk],1.);

end;

run;

Thanks~~

There is no way easy way to get SAS to do what you want. The problem will

coms in since you have some vars that are true character variables, some

that are true numerics held as characters, and here is the problem some that

are a mixture of character and numeric and right fully held as character.

If I understand you correctly you have all vars as character. Thus any mass

indescriminant conversion will give you errors in your log and not a good

error at that.

To do what you want all you need is a few simple things, first a list of the

numeric variables held as text that you want converted to numeric, second a

rename statement, length statement,, a drop statement on th eoutput side of

things, and lastly an assignment statement.

data one ;

infile cards dsd dlm = ' ' ;

input x1 $ x2 $ x3 $ x4 $ ;

cards ;

A 1 1 1

B 2 B 2

C 3 C 3

D 4 4 4

;

run ;

data two (drop = _:) ;

length x2 x4 8. ;

set one (rename = (x2 = _x2 x4 = _x4)) ;

x2 = _x2 ;

x4 = _x4 ;

run ;

proc contents ;

run ;

Now if you have a lot of vars you need converted this process can be made

easier by the use of either macros and/or arays.

Toby Dunn

From: "subscribe sas-L Chen, Chiu-Chen" < XXXX@XXXXX.COM >

Reply-To: "subscribe sas-L Chen, Chiu-Chen" < XXXX@XXXXX.COM >

To: XXXX@XXXXX.COM

Subject: How to transfer character variables into numeric variables by

using DO LOOP

Date: Sun, 24 Jul 2005 04:57:04 -0400

I have some mixed columns in an Excel data. So I read all variables into

SAS as character variables. Now I want to transfer all character variables

to numerical variables, but how should I write the programI wrote the

following program, but it doesn't work. Can anybody tell me where is wrong

or how to write correctly

data chen;

set chen;

array XXX[*] _character_;

do kk=1 to DIM(XXX);

XXX[kk]=input(XXX[kk],1.);

end;

run;

Thanks~~

Hi Chen,

I have a macro that does that, while "preserving" original variable names

and attributes like labels. You can specify more than one variable at a

time, while you may also specify no-numeric values and their corresponding

new numeric values to be replaced by.

See: http://www.yqcomputer.com/

and in the ZIP file the .SAS macro.

Regards - Jim.

My computer always teaches me something new I thought I knew already.

[common disclaimer]

On Sun, 24 Jul 2005 04:57:04 -0400, subscribe sas-L Chen, Chiu-Chen

I have a macro that does that, while "preserving" original variable names

and attributes like labels. You can specify more than one variable at a

time, while you may also specify no-numeric values and their corresponding

new numeric values to be replaced by.

See: http://www.yqcomputer.com/

and in the ZIP file the .SAS macro.

Regards - Jim.

My computer always teaches me something new I thought I knew already.

[common disclaimer]

On Sun, 24 Jul 2005 04:57:04 -0400, subscribe sas-L Chen, Chiu-Chen

1. convert a numeric variable to a character variable

2. putting the values of 5 different numeric variables into one numeric variable

3. RECODE STRING VARIABLES INTO NUMERIC VARIABLES WITHOUT CREATING NEW VARIABLES

4. RECODE STRING VARIABLES INTO NUMERIC VARIABLES WITHOUT CREATING NEW VARIABLES

5. Global variables / special variables / variable identifier with just one character

6. How to transform a group of char variables to numeric variables

7. Create Variables based on 5 other Numeric variables (decending order across)

8. converting string variable (time) into numeric variable

9. thousands of numeric variables convert to charector variables

10. data from 5 datasets with a numeric variable(out of 5 variables)

11. renaming string variables into numeric variables

12. How to combine 6 numeric variables into one new variable?

13. How to convert Display Variable to Numeric Signed Variable

14. Create Variables based on 5 other Numeric variables

15. Create Variables based on 5 other Numeric variables

5 post • Page:**1** of **1**