Encode character variables


If you want to use CHAR2FMT, then feel free to download it here:




If you are not prompted to "Save To Disk", then right-click the link and choose "Save Link Target As..."  Otherwise, you will need to save the web page to your computer.  Make sure you save the file as a plain text file not an htm/html file.


The is a plain text file that can be edited in any word processing software.  If you edit the file, save it as a text file.


Disclaimer:  There is no warranty on this software either expressed or implied.  This program is released under the terms and conditions of GNU General Public License.



Programmer:  Dan Blanchette
The Carolina Population Center
The University of North Carolina
Chapel Hill, NC  USA

Date:  20Aug2005
Last updated:  09Mar2009

Convert character variables to numeric


%char2fmt(dset=dataset in WORK , varlist= variable list , maxlen= max length of variables to convert );



The CHAR2FMT SAS macro converts character variables to numeric and creates SAS formats to store the character information.  This is like the Stata command encode.

If the character variable has a character format/value label then CHAR2FMT uses the actual/non-formatted variable values for the new numeric variable and ignores the character format/value label.

You could write the following SAS code:

 data (drop= relation);
  set work.old;
   if relation = "Mother" then relation_n = 1;
   else if relation = "Father" then relation_n = 2;
   else if relation = "Sister" then relation_n = 3;
   else if relation = "Brother" then relation_n = 4;

   ** make the label of the new numeric variable the same as the character one **;
   relation_n = "Relationship to respondent";

 proc format;
  value relation 
   1 = "Mother"
   2 = "Father"
   3 = "Sister"
   4 = "Brother"

 data new;
  set new;
   rename relation_n = relation;
   ** assign the format relation. to the variable relation **;
   format relation relation.;

Or you could use CHAR2FMT to do it for you:
   %char2fmt(dset= myData, varlist= relation);



dset= dataset_name

Name of dataset (no libref) in the WORK library in which variables are to be encoded.  The dataset has to be in the WORK library.

varlist= variables to encode

List of variables to encode to numeric variables no matter what length they are.  If a varlist is specified and maxlen= is also specified, then only variables in the list that have lengths longer than what is specified in maxlen= will be encoded to numeric variables.

maxlen= some number

The maximum length a character variable can be and _not_ be converted to numeric.  Basically, CHAR2FMT was written with the intention to help maintain long character variables (longer than 244 characters) when converting your data to Stata.


How to use the CHAR2FMT macro:

Using the CHAR2FMT SAS macro requires that you understand how to use the %include statement and that you know how to call a SAS macro.

For example, if you have copied this file to "C:\SASmacros", then you tell SAS about this macro by adding the following line to your SAS program:
  %include "C:\SASmacros\";

The include statement makes SAS aware of the CHAR2FMT SAS macro which is in the file  You call the macro by adding this to your code:
  %char2fmt(dset= myData, varlist= companyname location alliancetype );



 %include "C:\SASmacros\"; ** Make SAS aware of the CHAR2FMT macro.**;

  set work.old;

 ** now call the CHAR2FMT SAS macro by feeding it your dataset name and the
  * maximum length a character variable can be without getting selected 
  * for conversion.  A comma separates  what you feed the CHAR2FMT SAS macro.  **;

  %char2fmt(dset= new, maxlen= 120 );

  ** or all character variables (by not specifying maxlen= ): **;
  set work.old;

 %char2fmt(dset= new); 

 proc contents data=;

  ** or only the specified character variables: **;
  set work.old;

 %char2fmt(dset= new, varlist= location companyname ); 


Wink Plone Theme by Quintagroup © 2013.

Personal tools
This is themeComment for Wink theme