<?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: How do you convert a large amount of character variables to numerical variables? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-do-you-convert-a-large-amount-of-character-variables-to/m-p/279877#M56464</link>
    <description>&lt;P&gt;Thank you, I followed your advice and changed things at the start it self.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 23 Jun 2016 23:53:56 GMT</pubDate>
    <dc:creator>aaou</dc:creator>
    <dc:date>2016-06-23T23:53:56Z</dc:date>
    <item>
      <title>How do you convert a large amount of character variables to numerical variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-you-convert-a-large-amount-of-character-variables-to/m-p/279652#M56376</link>
      <description>&lt;P&gt;Hi, I have a data set with a large amount of character variables; I want to convert them to numerical variables.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can someone show me how to do this? I'm assuming you have to use arrays.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For ease of exposition suppose I have ten character variables.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 23 Jun 2016 05:35:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-you-convert-a-large-amount-of-character-variables-to/m-p/279652#M56376</guid>
      <dc:creator>aaou</dc:creator>
      <dc:date>2016-06-23T05:35:12Z</dc:date>
    </item>
    <item>
      <title>Re: How do you convert a large amount of character variables to numerical variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-you-convert-a-large-amount-of-character-variables-to/m-p/279654#M56377</link>
      <description>&lt;P&gt;If &lt;U&gt;all&lt;/U&gt; character variables carry numeric values (or are empty), then you can use this as a blueprint:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
set have;
array charvar {*} _character_;
call symput('numvars',strip(put(dim(charvar),best.)));
stop;
run;

data want;
set have;
array charvar {*} _character_;
array newvar {&amp;amp;numvars} newvar1-newvar&amp;amp;numvars;
do i = 1 to &amp;amp;numvars;
  newvar{i} = input(charvar{i},best.);
end;
drop i;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 23 Jun 2016 05:48:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-you-convert-a-large-amount-of-character-variables-to/m-p/279654#M56377</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-06-23T05:48:18Z</dc:date>
    </item>
    <item>
      <title>Re: How do you convert a large amount of character variables to numerical variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-you-convert-a-large-amount-of-character-variables-to/m-p/279655#M56378</link>
      <description>&lt;P&gt;Assume that you have 10 char variables(char1-char10), then you could try the below untested code&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;which creates 10 numeric variables&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data want;
set have;
array ch(10) char1-char10;
array nu(10) num1-num10;
do i = 1 to 10;
nu(i)=input(ch(i),best.);
end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 23 Jun 2016 05:49:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-you-convert-a-large-amount-of-character-variables-to/m-p/279655#M56378</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2016-06-23T05:49:19Z</dc:date>
    </item>
    <item>
      <title>Re: How do you convert a large amount of character variables to numerical variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-you-convert-a-large-amount-of-character-variables-to/m-p/279660#M56380</link>
      <description>&lt;P&gt;Hi thanks...I'm new to programming, so struggling a bit in understanding your code.&lt;/P&gt;&lt;P&gt;If you assume that the names of the character variables that I'm trying to convert are X,Y and Z...would you mind subsituting those variable names&amp;nbsp;in their respective places in the code you have posted? Just so that I can clearly understand what needs to be done. Thank you so much!&lt;/P&gt;</description>
      <pubDate>Thu, 23 Jun 2016 06:06:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-you-convert-a-large-amount-of-character-variables-to/m-p/279660#M56380</guid>
      <dc:creator>aaou</dc:creator>
      <dc:date>2016-06-23T06:06:11Z</dc:date>
    </item>
    <item>
      <title>Re: How do you convert a large amount of character variables to numerical variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-you-convert-a-large-amount-of-character-variables-to/m-p/279663#M56382</link>
      <description>&lt;P&gt;OK, lets dissect my code a little:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* the first step gets the number of character variables present in the
dataset, so I can later define the array of new variables with the correct
number of members */

data _null_; * do not create an output dataset;
set have;
array charvar {*} _character_; * define an array that contains all character variables,
 no need to know their names;;
call symput('numvars',strip(put(dim(charvar),best.))); * put the value into a macro variable;
stop; * end execution in the first iteration of the data step;
run;

data want;
set have;
array charvar {*} _character_; * see above;
array newvar {&amp;amp;numvars} newvar1-newvar&amp;amp;numvars;* here I have to define names for the new (numeric) variables,
for the size I use the macro variable created in the first step;
* also note that the default for a newly defined array is numeric;
do i = 1 to &amp;amp;numvars; * iterate through both arrays;
  newvar{i} = input(charvar{i},best.); * convert;
end;
drop i; * get rid of the index variable;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 23 Jun 2016 06:25:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-you-convert-a-large-amount-of-character-variables-to/m-p/279663#M56382</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-06-23T06:25:33Z</dc:date>
    </item>
    <item>
      <title>Re: How do you convert a large amount of character variables to numerical variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-you-convert-a-large-amount-of-character-variables-to/m-p/279665#M56384</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/91209"&gt;@aaou&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Hi thanks...I'm new to programming, so struggling a bit in understanding your code.&lt;/P&gt;
&lt;P&gt;If you assume that the names of the character variables that I'm trying to convert are X,Y and Z...would you mind subsituting those variable names&amp;nbsp;in their respective places in the code you have posted? Just so that I can clearly understand what needs to be done. Thank you so much!&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;If you do not have a naming pattern for your variables (that enables simple iteration with an index), it makes things more complicated. To fully automate the task, you will need to use the dataset metadata to create a list of variables to be converted.&lt;/P&gt;
&lt;P&gt;Assume you have stored the library and dataset name in macro variables:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
set sashelp.vcolumn (
  where=(libname = "&amp;amp;mylibname" and memname = "&amp;amp;mydataset" and type = 'char')
) end=done;
/* get data from the view in SASHELP that describes columns;
take only those from your dataset with type character */
/* also define a variable that signals the end */
if _n_ = 1 then call execute("data &amp;amp;mylibname..&amp;amp;mydataset._num; set &amp;amp;mylibname..&amp;amp;mydataset;");
/* call execute pushes code into the execution queue to be executed
immediately after the current data step ends */
/* this one starts a data step */
call execute(trim(name)!!'_num = input('!!trim(name)!!',best.);');
/* do the conversions */
if done then call execute('run;');
/* finish the data step */
run;

 &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 23 Jun 2016 06:52:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-you-convert-a-large-amount-of-character-variables-to/m-p/279665#M56384</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-06-23T06:52:24Z</dc:date>
    </item>
    <item>
      <title>Re: How do you convert a large amount of character variables to numerical variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-you-convert-a-large-amount-of-character-variables-to/m-p/279691#M56394</link>
      <description>&lt;P&gt;This is clearly and example of bad data strcuture. &amp;nbsp;Why do you have "a large amount of character variables" on which you need to do this processing? &amp;nbsp;Why not have your data elements going fown the page as observations, it makes almost all programming tasks so much easier:&lt;/P&gt;
&lt;PRE&gt;data have;
  x="1"; y="2"; z="3";
run;

proc transpose data=have out=t_have;
  var x y z;
run;

/* First example you need to know all the variables */
data want1 (drop=i);
  set have;
  array xyz{3} x y z;
  array xyz_num{3} 8;
  do i=1 to 3;
    xyz_num{i}=input(xyz{i},best.);
  end;
run;

/* Second example you only need to know there is two columns */
data want2;
  set t_have
  xyz_num=input(col1,best.);
run;&lt;/PRE&gt;
&lt;P&gt;Remember the data you work with and program with does NOT need to be the same as what is in the output - make life simple for you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 23 Jun 2016 08:30:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-you-convert-a-large-amount-of-character-variables-to/m-p/279691#M56394</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-06-23T08:30:40Z</dc:date>
    </item>
    <item>
      <title>Re: How do you convert a large amount of character variables to numerical variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-you-convert-a-large-amount-of-character-variables-to/m-p/279851#M56447</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Converting character data to numeric

HAVE

 Variables in Creation Order

#    Variable    Type    Len

1    X           Char      1
2    Y           Char      1
3    Z           Char      1

WANT

 Variables in Creation Order

#    Variable    Type    Len

1    X           Num       8
2    Y           Num       8
3    Z           Num       8


* create some data;
data have;
  x="1"; y="2"; z="3";
run;

* create the select clause to covert char to num;
proc sql;
  select
     catx(' ','input(',name,',best.) as',name) into :namlst separated by ','
  from
     sashelp.vcolumn
  where
          libname='WORK'
     and  memname='HAVE'
     and  upcase(type) eqt 'C'
;quit;

%put &amp;amp;=namlst;
/*
 input( X ,best.) as X
,input( Y ,best.) as Y
,input( Z ,best.) as Z
*/

* do the conversion;
proc sql;
  create
    table want as
  select
    &amp;amp;namlst
  from
    have;
;quit;

/*
       X         Y         Z
----------------------------
       1         2         3
*/


&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 23 Jun 2016 20:47:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-you-convert-a-large-amount-of-character-variables-to/m-p/279851#M56447</guid>
      <dc:creator>rogerjdeangelis</dc:creator>
      <dc:date>2016-06-23T20:47:13Z</dc:date>
    </item>
    <item>
      <title>Re: How do you convert a large amount of character variables to numerical variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-you-convert-a-large-amount-of-character-variables-to/m-p/279876#M56463</link>
      <description>&lt;P&gt;Thanks a lot! Truly appreciate it.&lt;/P&gt;</description>
      <pubDate>Thu, 23 Jun 2016 23:53:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-you-convert-a-large-amount-of-character-variables-to/m-p/279876#M56463</guid>
      <dc:creator>aaou</dc:creator>
      <dc:date>2016-06-23T23:53:00Z</dc:date>
    </item>
    <item>
      <title>Re: How do you convert a large amount of character variables to numerical variables?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-you-convert-a-large-amount-of-character-variables-to/m-p/279877#M56464</link>
      <description>&lt;P&gt;Thank you, I followed your advice and changed things at the start it self.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 23 Jun 2016 23:53:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-you-convert-a-large-amount-of-character-variables-to/m-p/279877#M56464</guid>
      <dc:creator>aaou</dc:creator>
      <dc:date>2016-06-23T23:53:56Z</dc:date>
    </item>
  </channel>
</rss>

