DATA Step, Macro, Functions and more

convert characters to numeric

Occasional Contributor
Posts: 15

convert characters to numeric


In my original dataset, I have gender mentioned as Male and Female and therefore i'm unable to use PROC MEANS or UNIVARIATE. When i converted female=0 and male= 1 in the original data set, i was able to run these procedures. But it's  cumbersome process since i have 90 other such variables that are in character form.


Two questions:


1. How can i convert charac into numeric form in SAS


If i have to convert zones, for example- north, south, east and west to 0,1,2 and 3 respectively, what will the code look like?



2. Is there a way by which i can run Means or univariate or other such commands with the charac variables?

Posts: 1,215

Re: convert characters to numeric

Posted in reply to Arushiarora0

What are you trying to do with this categorical variable? You use proc univariate for a continuous numeric variable. With a categorical variable such as gender/sec, use proc freq


proc freq data = sashelp.class;
	tables sex;

Examples are given in the documentation here

Super User
Posts: 9,905

Re: convert characters to numeric

Posted in reply to Arushiarora0

If you need to convert a categorical variable to numeric (limited cardinality!), use formats:

data have;
input zone $;

%let varname=zone;

proc sort
  data=have (
by start;

data cntlin;
set cntlin;
fmtname = "_&varname._char";
type = 'I';
label = _n_;

data cntlin2;
set cntlin (rename=(label=start start=label));
fmtname = "_&varname._num";
type = "N";

proc format library=work cntlin=cntlin;

proc format library=work cntlin=cntlin2;

data want;
set have;
_&varname._num = input(&varname,_&varname._char.);
format _&varname._num _&varname._num.;

proc sql;
/* just to show that the new variable really is numeric,
   although it displays as character values */
select sum(_&varname._num) from want;

Note that you only have to set the variable name once, so you can easily use this procedure repeatedly (call execute) from a dataset that contains your variable names (retrieve from dictionary.columns/sashelp.vcolumn).

Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Super User
Super User
Posts: 9,423

Re: convert characters to numeric

Posted in reply to Arushiarora0

This is in relation to your other question regarding importing data from Excel isn't it.  You could save yourself a whole lot of effort by removing the two obstacles in your path - 1) Excel, which is an unstructured file format, 2) Proc import, which is a guessing procedure.  These two combined will make your data garbage.  I would advise:

1) Save to CSV file format, this is a text based file format.  Save as from Excel.

2) Take the proc import code, or write it yourself, a datastep import where you specify how each data element should be read in.  It would look something like:

data want;
  infile "your_csv_file.csv" dlm=",";
  length ...;
  informat ...;
  format ...;
  input ...;

Obviously the ... would be replaced by the lists of variables to be read in.  In this way you can a) fix the data as it comes into SAS into character/numeric as you know the data to be, apply formatting - such as Genders, and do any other process, all in one simple step avoiding Excel "features" and proc import trying to guess what you want to do.


As a final tip, if this is Pharma data, then perhaps look at the CDISC models, by mapping to those you would put values and codes in the dataset so you would have everything ready for analysis.

Ask a Question
Discussion stats
  • 3 replies
  • 4 in conversation