<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Recoding a matrix of character variables into numeric in SAS Data Management</title>
    <link>https://communities.sas.com/t5/SAS-Data-Management/Recoding-a-matrix-of-character-variables-into-numeric/m-p/233893#M5872</link>
    <description>&lt;P&gt;One efficient way to do this kind of recoding is with an informat:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data myInFormat;
set dat2;
fmtname = "myFmt";
type = "I";
start = cv;
label = cats(nv);
run;

proc format cntlin=myInFormat; run;

data dat3;
set dat1;
array cv{4};
array nv{4};
do i = 1 to dim(cv);
    nv{i} = input(cv{i}, myFmt.);
    end;
drop cv:;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 09 Nov 2015 19:08:49 GMT</pubDate>
    <dc:creator>PGStats</dc:creator>
    <dc:date>2015-11-09T19:08:49Z</dc:date>
    <item>
      <title>Recoding a matrix of character variables into numeric</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Recoding-a-matrix-of-character-variables-into-numeric/m-p/233887#M5870</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a dataset (dat1)&amp;nbsp;that has&amp;nbsp;an identifier (numeric) as the first variable and n character variables. I also have another dataset &lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;dat2)&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&amp;nbsp;that maps each possible entry&amp;nbsp;in all the character variables to a numeric value.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data dat1;
input id cv1 $ cv2 $ cv3 $ cv4 $;
datalines;
111 aaa bbb ccc ccc
112 bbb bbb aaa ddd
113 aaa ccc ccc aaa
114 ddd bbb eee bbb
;

data dat2;
input cv $ nv;
datalines;
aaa 1
bbb 2
ccc 3
ddd 4
eee 5
;&lt;/PRE&gt;&lt;P&gt;I'd like to turn&amp;nbsp;all the character variables in dat1 to numeric using the mapping in dat2. Therefore, the resulting dataset in my example should be like:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;data dat3;
input id nv1 nv2 nv3 nv4;
datalines;
111 1 2 3 3 
112 2 2 1 4 
113 1 3 3 1
114 4 2 5 2 
;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Obviously, my true dat1 dataset is huge.&lt;/P&gt;&lt;P&gt;Thanks.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Nov 2015 18:45:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Recoding-a-matrix-of-character-variables-into-numeric/m-p/233887#M5870</guid>
      <dc:creator>Emanuele_m</dc:creator>
      <dc:date>2015-11-09T18:45:42Z</dc:date>
    </item>
    <item>
      <title>Re: Recoding a matrix of character variables into numeric</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Recoding-a-matrix-of-character-variables-into-numeric/m-p/233892#M5871</link>
      <description>&lt;P&gt;Not really possible to change data type from character to numeric directly. One way is to create a format and reread the old values into new variables using the values in dat2 to create an informat.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data dat2;
input cv $ nv;
datalines;
aaa 1
bbb 2
ccc 3
ddd 4
eee 5
;
run;

data cvcntlin;
   set dat2;
   FMTName= 'Cv2Num';
   start=cv;
   label=nv;
   type='I';
run;

proc format
library=work cntlin=cvcntlin;
run; 

data dat1;
input id cv1 $ cv2 $ cv3 $ cv4 $;
datalines;
111 aaa bbb ccc ccc
112 bbb bbb aaa ddd
113 aaa ccc ccc aaa
114 ddd bbb eee bbb
;
run;

data want;
   set dat1 (rename=(cv1=_cv1 cv2=_cv2 cv3=_cv3 cv4=_cv4 ));
   array old _cv: ;
   array new cv1 - cv4;
   do i=1 to dim(old);
      new(i) = input(old[i],CV2num.);
   end;
   drop _cv i;
run;
   
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 09 Nov 2015 19:04:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Recoding-a-matrix-of-character-variables-into-numeric/m-p/233892#M5871</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2015-11-09T19:04:17Z</dc:date>
    </item>
    <item>
      <title>Re: Recoding a matrix of character variables into numeric</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Recoding-a-matrix-of-character-variables-into-numeric/m-p/233893#M5872</link>
      <description>&lt;P&gt;One efficient way to do this kind of recoding is with an informat:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data myInFormat;
set dat2;
fmtname = "myFmt";
type = "I";
start = cv;
label = cats(nv);
run;

proc format cntlin=myInFormat; run;

data dat3;
set dat1;
array cv{4};
array nv{4};
do i = 1 to dim(cv);
    nv{i} = input(cv{i}, myFmt.);
    end;
drop cv:;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 09 Nov 2015 19:08:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Recoding-a-matrix-of-character-variables-into-numeric/m-p/233893#M5872</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2015-11-09T19:08:49Z</dc:date>
    </item>
    <item>
      <title>Re: Recoding a matrix of character variables into numeric</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Recoding-a-matrix-of-character-variables-into-numeric/m-p/233942#M5873</link>
      <description>&lt;P&gt;Would somebody please post a hashing solution ... I can figure it out if I have to but it should be simple for someone who is used to doing this.&lt;/P&gt;</description>
      <pubDate>Mon, 09 Nov 2015 23:49:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Recoding-a-matrix-of-character-variables-into-numeric/m-p/233942#M5873</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2015-11-09T23:49:18Z</dc:date>
    </item>
  </channel>
</rss>

