Learning SAS? Welcome to the exclusive online community for all SAS learners.

Converting character to numeric for dummies

Accepted Solution Solved
Reply
New Contributor
Posts: 4
Accepted Solution

Converting character to numeric for dummies

I have a column of data with the following attributes:

 

Data Set Name: WORK.IMPORT

Variable: Kill_wt

Type: Char

Len: 14

Format: $14

Informat: $14

Label: Kill wt

 

These data are continuous numerical data, but SAS doesn't see it this way. I tried the following command from a Youtube video to try and correct this:

 

data WORK.IMPORT2; set WORK.IMPORT;
* Convert Kill_wt from character to numeric;
Kill_wt = input(Kill_wt, best12.);
run;

 

No errors show up when I run this, but nothing changes. How can I change the format/informat for "Kill_wt" from $14(character) to BEST12(this is what all my numerical data in the set are set as)? Please explain it to me like I have only been using SAS for a week (I have).

 

Thanks!


Accepted Solutions
Solution
‎08-07-2017 11:25 AM
Super User
Posts: 19,868

Re: Converting character to numeric for dummies

[ Edited ]

Your code is correct, with one major issue, you can't convert a variable from text to numeric and keep the same name.

That would be changing the variable type on the fly and SAS doesn't support that.

 

So...either rename the original variable on your SET statement or your calculated variable.

 

kill_wt2 = input(kill_wt, best12.)

 

This SAS Note contains lots of helpful advice for converting data types in SAS: 

 

View solution in original post


All Replies
Solution
‎08-07-2017 11:25 AM
Super User
Posts: 19,868

Re: Converting character to numeric for dummies

[ Edited ]

Your code is correct, with one major issue, you can't convert a variable from text to numeric and keep the same name.

That would be changing the variable type on the fly and SAS doesn't support that.

 

So...either rename the original variable on your SET statement or your calculated variable.

 

kill_wt2 = input(kill_wt, best12.)

 

This SAS Note contains lots of helpful advice for converting data types in SAS: 

 

New Contributor
Posts: 4

Re: Converting character to numeric for dummies

[ Edited ]

Thanks Reeza, but I tried that to no avail. Any other ideas? Could it be how I imported the data from .xls?

Super User
Posts: 19,868

Re: Converting character to numeric for dummies

Post your code, log and proc contents from new data set. That should have worked.

New Contributor
Posts: 4

Re: Converting character to numeric for dummies

/* Generated Code (IMPORT) */
/* Source File: Aspirin field trial.xls */
/* Source Path: /folders/myfolders */
/* Code generated on: Thursday, January 28, 2016 3:26:09 AM */

%web_drop_table(WORK.IMPORT);


FILENAME REFFILE "/folders/myfolders/Aspirin field trial.xls" TERMSTR=CR;

PROC IMPORT DATAFILE=REFFILE
DBMS=XLS
OUT=WORK.IMPORT;
GETNAMES=YES;
RUN;

PROC CONTENTS DATA=WORK.IMPORT; RUN;


%web_open_table(WORK.IMPORT);

data WORK.IMPORT2; set WORK.IMPORT;
* Convert kill_wt from character to numeric;
Lung_wt2 = input(Lung_wt, best9.);
run;

 

1 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
55
56 /* Generated Code (IMPORT) */
57 /* Source File: Aspirin field trial.xls */
58 /* Source Path: /folders/myfolders */
59 /* Code generated on: Thursday, January 28, 2016 3:26:09 AM */
60
61 %web_drop_table(WORK.IMPORT);
NOTE: Table WORK.IMPORT has been dropped.
NOTE: PROCEDURE SQL used (Total process time):
real time 0.00 seconds
cpu time 0.00 seconds
 
 
62
63
64 FILENAME REFFILE "/folders/myfolders/Aspirin field trial.xls" TERMSTR=CR;
65
66 PROC IMPORT DATAFILE=REFFILE
67 DBMS=XLS
68 OUT=WORK.IMPORT;
69 GETNAMES=YES;
70 RUN;
 
NOTE: Variable Name Change. Ear tag -> Ear_tag
NOTE: Variable Name Change. Entry wt -> Entry_wt
NOTE: Variable Name Change. Treat d1 -> Treat_d1
NOTE: Variable Name Change. Treat d3 -> Treat_d3
NOTE: Variable Name Change. Treat d6 -> Treat_d6
NOTE: Variable Name Change. Lung wt -> Lung_wt
NOTE: Variable Name Change. Kill wt -> Kill_wt
NOTE: The import data set has 244 observations and 21 variables.
NOTE: WORK.IMPORT data set was successfully created.
NOTE: PROCEDURE IMPORT used (Total process time):
real time 0.06 seconds
cpu time 0.04 seconds
 
 
71
72 PROC CONTENTS DATA=WORK.IMPORT; RUN;
 
NOTE: PROCEDURE CONTENTS used (Total process time):
real time 0.18 seconds
cpu time 0.18 seconds
 
 
73
74
75 %web_open_table(WORK.IMPORT);
76
77 data WORK.IMPORT2; set WORK.IMPORT;
78 * Convert kill_wt from character to numeric;
79 Lung_wt2 = input(Lung_wt, best9.);
80 run;
 
NOTE: There were 244 observations read from the data set WORK.IMPORT.
NOTE: The data set WORK.IMPORT2 has 244 observations and 22 variables.
NOTE: DATA statement used (Total process time):
real time 0.02 seconds
cpu time 0.02 seconds
 
 
81
82
83
84
85 OPTIONS NONOTES NOSTIMER NOSOURCE NOSYNTAXCHECK;
97
 
PROC SQL;
CREATE TABLE WORK.query AS
SELECT Ear_tag , Entry_wt , Gender , 'Cast'n , DH , Treat_d1 , Treat_d3 , Treat_d6 , Lung_wt , Kill_wt , 'Group'n , ADG , Fibrin , Fibrosis , Lesion , Plant , Feedlot , S_3MI_D0 , S_3MI_D3 , S_3MI_D6 , R_3MI FROM WORK.IMPORT;
RUN;
QUIT;

PROC DATASETS NOLIST NODETAILS;
CONTENTS DATA=WORK.query OUT=WORK.details;
RUN;

PROC PRINT DATA=WORK.details;
RUN;
 
I'm not sure where my proc contents are, but hopefully this helps.
Super User
Posts: 19,868

Re: Converting character to numeric for dummies

[ Edited ]

Your last step references import not the new data set you created, import2.

Change it to refer to import2 and change the select to include lung_wt2.

 

Basically you created the correct dataset but didn't use it in your next steps, but went back to the original dataset.

 

Manually check the data set import2 -> open it in the editor.

 

New Contributor
Posts: 4

Re: Converting character to numeric for dummies

I hate to ask this, you have been so patient and helpful, but which step am I changing to import2?
Super User
Posts: 19,868

Re: Converting character to numeric for dummies

proc sql, the last step. 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 7610 views
  • 0 likes
  • 2 in conversation