LISTSERV at the University of Georgia
Menubar Imagemap
Home Browse Manage Request Manuals Register
Previous messageNext messagePrevious in topicNext in topicPrevious by same authorNext by same authorPrevious page (October 2003, week 3)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:         Mon, 20 Oct 2003 10:35:36 -0400
Reply-To:     Sigurd Hermansen <HERMANS1@WESTAT.COM>
Sender:       "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:         Sigurd Hermansen <HERMANS1@WESTAT.COM>
Subject:      Re: How to convert convert character variables
Comments: To: "Groeneveld, Jim" <jim.groeneveld@VITATRON.COM>
Content-Type: text/plain

Jim: We can build on your solution and answer another question as well. A SQL solution handles the type conversion without explicit renaming (and, with the LEFT() function, aligns the character values consistently): proc sql; create table c as (select d1b1 as d1b1,d1b2 as d1b2,d1b3 as d1b3 from a ) outer union corr (select left(put(d1b1,3.)) as d1b1, left(put(d1b2,3.)) as d1b2, left(put(d1b3,3.)) as d1b3 from b ) ; quit;

The SQL solution also answers Tine Andersen's question. Is there a way to make SAS programs more aesthetically pleasing? Yes, use PROC SQL. ;> (I wonder what it would take to convert SAS Data steps automatically, where equivalent, to SQL queries?) Sig

-----Original Message----- From: Groeneveld, Jim [mailto:jim.groeneveld@VITATRON.COM] Sent: Monday, October 20, 2003 6:04 AM To: SAS-L@LISTSERV.UGA.EDU Subject: Re: How to convert convert character variables

Hi Italian nameless Ebola,

Can't you be a little bit less anonymous? You do not need to be ashamed asking questions like this. That is where the list and all of us are for. Back to your problem:

Before getting that error report you get important notes as well, saying:

NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column). 11:13 NOTE: Numeric values have been converted to character values at the places given by: (Line):(Column). 11:1 NOTE: Invalid numeric data, 'a' , at line 11 column 13. NOTE: Invalid numeric data, 'b' , at line 11 column 13. NOTE: Invalid numeric data, 'c' , at line 11 column 13. d1b1=. d1b2=. d1b3=. k=4 _ERROR_=1 _N_=1 NOTE: Missing values were generated as a result of performing an operation on missing values. Each place is given by: (Number of times) at (Line):(Column). 3 at 11:21

From that you already could guess that it is impossible to convert a variable's type. You would have to create new (numeric) variables (and possible additional arrays). But it makes no sense to convert the character a, b and c to numeric, so in order to obtain your frequency table you better could create a character variable from you numeric ones:

data a; d1b1 = "a"; d1b2 = "b"; d1b3 = "c"; RUN;

data b; d1b1 = 1; d1b2 = 2; d1b3 = 3; RUN;

DATA B (DROP=d1b1 d1b2 d1b3); SET B; LENGTH d0b1 - d0b3 $1; d0b1 = PUT (d1b1, 1.); d0b2 = PUT (d1b2, 1.); d0b3 = PUT (d1b3, 1.); RUN;

data c; set a b (RENAME=(d0b1=d1b1 d0b2=d1b2 d0b3=d1b3)); RUN;

proc freq; table d1b1-d1b3; run;

Regards - Jim. -- . . . . . . . . . . . . . . . .

Jim Groeneveld, MSc. Biostatistician Science Team Vitatron B.V. Meander 1051 6825 MJ Arnhem Tel: +31/0 26 376 7365 Fax: +31/0 26 376 7305 Jim.Groeneveld@Vitatron.com www.vitatron.com

I wish I had a webcam to show you the boring view from my window.

[common disclaimer]

-----Original Message----- From: ebola [mailto:nameless20102003@YAHOO.IT] Sent: Monday, October 20, 2003 09:46 To: SAS-L@LISTSERV.UGA.EDU Subject: How to convert convert character variables

I am tring to transform character variables to numeric. Here is a small exemple

data a;

d1b1 = "a"; d1b2 = "b"; d1b3 = "c";

/* here I tried the transformation */

array varc1 (*) d1b1-d1b3 ; do k = 1 to dim (varc1); varc1 (k) = varc1(k)+0 ; /* or this varc1 (k) = INPUT(varc1(k), 3.); */ end;

data b;

d1b1 = 1; d1b2 = 2; d1b3 = 3;

data c; set a b;

proc freq; table

d1b1-d1b3;

run;

and I get this Log message

336 data c; 337 set a b; ERROR: Variable d1b1 has been defined as both character and numeric. ERROR: Variable d1b2 has been defined as both character and numeric. ERROR: Variable d1b3 has been defined as both character and numeric. 338

NOTE: The SAS System stopped processing this step because of errors. WARNING: The data set WORK.C may be incomplete. When this step was stopped there were 0 observations and 4 variables. WARNING: Data set WORK.C was not replaced because this step was stopped. NOTE: DATA statement used: real time 0.00 seconds cpu time 0.00 seconds

Thankyou in advance for any help.


Back to: Top of message | Previous page | Main SAS-L page