<?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: Converting Char to Numeric in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Converting-Char-to-Numeric/m-p/133495#M36247</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You can get there, although there are several pitfalls.&amp;nbsp; You noticed the first one, the number of variables, since a single macro variable may not be long enough to hold the names of all the XXXX variables.&amp;nbsp; Another pitfall is that some of the names may be long.&amp;nbsp; This particular macro depends on being able to create a new variable named NEW_oldname which will not be possible with longer variable names.&amp;nbsp; Here is one approach:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;filename myprog 'path to a file that can hold a SAS program';&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc contents data=have noprint out=xxxx_list (keep=name where=(upcase(name)=:'XXXX'));&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data _null_;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; file myprog noprint;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; set xxxx_list end=done;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; newname = 'v' || strip(put(_n_,4.));&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; put newname '= input(' name ', 16.);';&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; put 'drop ' name ';' ;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; put 'rename ' newname '=' name ';';&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;set have;&lt;/P&gt;&lt;P&gt;%include myprog;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The code is untested, so may need a small amount of debugging.&amp;nbsp; You may want to test it on a smaller data set that contains only a few XXXX variables.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I chose FILE/PUT rather than CALL EXECUTE because I thought it would be easier to visualize the code that you are generating.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Good luck.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 06 Nov 2013 19:15:38 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2013-11-06T19:15:38Z</dc:date>
    <item>
      <title>Converting Char to Numeric</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Converting-Char-to-Numeric/m-p/133489#M36241</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have several data sets that have more than 7000 variables in each and I need to convert Variables with prefix XXXX&amp;nbsp; from character to numeric type.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;What could be the optimal solution for this process? &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks in advance for your help.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 06 Nov 2013 16:55:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Converting-Char-to-Numeric/m-p/133489#M36241</guid>
      <dc:creator>Sas_R</dc:creator>
      <dc:date>2013-11-06T16:55:06Z</dc:date>
    </item>
    <item>
      <title>Re: Converting Char to Numeric</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Converting-Char-to-Numeric/m-p/133490#M36242</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;With that many, it may be easiest to write out to a (temporary) text file and read back in, if there are naming conveniences (like VAR1-VAR1000 or something, name-wise).&amp;nbsp; You don't provide much detail so it's hard to say exactly but that's often easier than setting up the renames.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 06 Nov 2013 17:11:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Converting-Char-to-Numeric/m-p/133490#M36242</guid>
      <dc:creator>snoopy369</dc:creator>
      <dc:date>2013-11-06T17:11:43Z</dc:date>
    </item>
    <item>
      <title>Re: Converting Char to Numeric</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Converting-Char-to-Numeric/m-p/133491#M36243</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You can try arrays to convert those many variables from character to numeric. Please check the sample code considering the variables with names var1 - var1000.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; set have;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; array chars(*) $ var1-var1000;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; array nums(*) num1-num1000;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; do i = 1 to dim(chars);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; nums(i)=input(chars(i),best.);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; drop var:;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Jagadish&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 06 Nov 2013 17:23:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Converting-Char-to-Numeric/m-p/133491#M36243</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2013-11-06T17:23:47Z</dc:date>
    </item>
    <item>
      <title>Re: Converting Char to Numeric</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Converting-Char-to-Numeric/m-p/133492#M36244</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;SAS doesn't allow this.&amp;nbsp; Once a variable is character, it remains character forever.&amp;nbsp; You will need to decide upon new names for the numeric versions.&amp;nbsp; It could be as simple as using YYYY instead of XXXX as the prefix, but that has to be your first step.&amp;nbsp; The second step would be to verify that the variables you wish to convert are short enough.&amp;nbsp; SAS has a limit of roughly 15 to 16 significant digits for accurate storage of integer values.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;After the conversion (and as part of the same DATA step), it would be possible to drop the XXXX variables and rename the YYYY variables to the old XXXX names.&amp;nbsp;&amp;nbsp; So the net effect would be that the original names become numeric instead of character.&amp;nbsp; All of this can be automated using macro language, but the first couple of steps are up to you.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Good luck.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 06 Nov 2013 17:30:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Converting-Char-to-Numeric/m-p/133492#M36244</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2013-11-06T17:30:13Z</dc:date>
    </item>
    <item>
      <title>Re: Converting Char to Numeric</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Converting-Char-to-Numeric/m-p/133493#M36245</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thank you for the feedback. Here is more details on what I'm trying to accomplish;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Originally, I tried to apply below macro which is automaticallt converts char to numeric data type with original names and labels which I need it for the dataset.&lt;/P&gt;&lt;P&gt;&lt;A href="http://www.nesug.org/Proceedings/nesug10/ff/ff01.pdf"&gt;http://www.nesug.org/Proceedings/nesug10/ff/ff01.pdf&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;However since I have too many variables ( around 4000 out of 7000 with Prefix XXXX) , this macro fails because of limit on variable counts.&lt;/P&gt;&lt;P&gt;I tried to modify the macro with variable creation number like first 1 to 500 and then 501 to 1000 and so on, but this is making process much more complex.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I was wondering if there is an optimal way of converting these variables from char to numeric with keeping their original name and labels ?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 06 Nov 2013 18:13:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Converting-Char-to-Numeric/m-p/133493#M36245</guid>
      <dc:creator>Sas_R</dc:creator>
      <dc:date>2013-11-06T18:13:56Z</dc:date>
    </item>
    <item>
      <title>Re: Converting Char to Numeric</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Converting-Char-to-Numeric/m-p/133494#M36246</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Just to be clear, it is not possible to _directly_ change a variable's type.&amp;nbsp; You must go through some intermediary, whether it be a differently named variable or a text file, to convert.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The general approach would be:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;1.&amp;nbsp; Actually convert them.&lt;/P&gt;&lt;P&gt;2.&amp;nbsp; Obtain the old labels/names and order.&lt;/P&gt;&lt;P&gt;3.&amp;nbsp; Apply the labels/names and order.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Step 1 can be done a number of different ways; something like&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;set have;&lt;/P&gt;&lt;P&gt;newvar1 = input(oldvar1,best12.);&lt;/P&gt;&lt;P&gt;rename newvar1=oldvar1;&lt;/P&gt;&lt;P&gt;drop oldvar1;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;accomplishes this.&amp;nbsp; You could macro-ize this:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%macro convert(var);&lt;/P&gt;&lt;P&gt;&amp;amp;var._new = input(&amp;amp;var.,best12.);&lt;/P&gt;&lt;P&gt;rename &amp;amp;var._new = &amp;amp;var.;&lt;/P&gt;&lt;P&gt;drop &amp;amp;var.;&lt;/P&gt;&lt;P&gt;%mend convert;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Then you could include some method of fixing the labels.&amp;nbsp; Here's an example.&amp;nbsp; This may not work perfectly because you may have to break up the two long macro variables (&amp;amp;namelist and &amp;amp;convertlist) if they are more than 20k characters long - but you can use varnum to filter that easily like you suggest (do first 500 then next 500 etc.)&amp;nbsp; Important is that &amp;amp;namelist contains ALL variables, and &amp;amp;convertlist contains the ones you are converting.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data class;&lt;/P&gt;&lt;P&gt;set sashelp.class;&lt;/P&gt;&lt;P&gt;newage=put(age,3.);&lt;/P&gt;&lt;P&gt;newheight=put(height,3.);&lt;/P&gt;&lt;P&gt;newweight=put(weight,3.);&lt;/P&gt;&lt;P&gt;rename&lt;/P&gt;&lt;P&gt;newage=age&lt;/P&gt;&lt;P&gt;newheight=height&lt;/P&gt;&lt;P&gt;newweight=weight&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;label&lt;/P&gt;&lt;P&gt;newage='Age of Person'&lt;/P&gt;&lt;P&gt;newheight='Height of Person'&lt;/P&gt;&lt;P&gt;newweight='Weight of Person'&lt;/P&gt;&lt;P&gt;;&lt;/P&gt;&lt;P&gt;drop age height weight;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%macro convert(var);&lt;/P&gt;&lt;P&gt;&amp;amp;var._new = input(&amp;amp;var.,best12.);&lt;/P&gt;&lt;P&gt;rename &amp;amp;var._new = &amp;amp;var.;&lt;/P&gt;&lt;P&gt;drop &amp;amp;var.;&lt;/P&gt;&lt;P&gt;callstr=cats("label &amp;amp;var.='",vlabel(&amp;amp;var.),"';");&lt;/P&gt;&lt;P&gt;if _n_ = 1 then call execute(callstr);&lt;/P&gt;&lt;P&gt;%mend convert;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;select name into :namelist separated by ' ' &lt;/P&gt;&lt;P&gt;from dictionary.columns&lt;/P&gt;&lt;P&gt;where libname='WORK' and memname='CLASS'&lt;/P&gt;&lt;P&gt;order by varnum;&lt;/P&gt;&lt;P&gt;select cats('%convert(',name,')') into :convertlist separated by ' '&lt;/P&gt;&lt;P&gt;from dictionary.columns&lt;/P&gt;&lt;P&gt;where libname='WORK' and memname='CLASS'&lt;/P&gt;&lt;P&gt;and upcase(name) in ('AGE','HEIGHT','WEIGHT');&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data class_n;&lt;/P&gt;&lt;P&gt;set class end=end;&lt;/P&gt;&lt;P&gt;if _n_ = 1 then do;&lt;/P&gt;&lt;P&gt;&amp;nbsp; call execute ("data class_n_f; retain &amp;amp;namelist.; set class_n;");&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;&amp;amp;convertlist;&lt;/P&gt;&lt;P&gt;if end then do;&lt;/P&gt;&lt;P&gt;&amp;nbsp; call execute("run;");&lt;/P&gt;&lt;P&gt;end;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 06 Nov 2013 18:34:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Converting-Char-to-Numeric/m-p/133494#M36246</guid>
      <dc:creator>snoopy369</dc:creator>
      <dc:date>2013-11-06T18:34:07Z</dc:date>
    </item>
    <item>
      <title>Re: Converting Char to Numeric</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Converting-Char-to-Numeric/m-p/133495#M36247</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You can get there, although there are several pitfalls.&amp;nbsp; You noticed the first one, the number of variables, since a single macro variable may not be long enough to hold the names of all the XXXX variables.&amp;nbsp; Another pitfall is that some of the names may be long.&amp;nbsp; This particular macro depends on being able to create a new variable named NEW_oldname which will not be possible with longer variable names.&amp;nbsp; Here is one approach:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;filename myprog 'path to a file that can hold a SAS program';&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;proc contents data=have noprint out=xxxx_list (keep=name where=(upcase(name)=:'XXXX'));&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;data _null_;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; file myprog noprint;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; set xxxx_list end=done;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; newname = 'v' || strip(put(_n_,4.));&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; put newname '= input(' name ', 16.);';&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; put 'drop ' name ';' ;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; put 'rename ' newname '=' name ';';&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;set have;&lt;/P&gt;&lt;P&gt;%include myprog;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The code is untested, so may need a small amount of debugging.&amp;nbsp; You may want to test it on a smaller data set that contains only a few XXXX variables.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I chose FILE/PUT rather than CALL EXECUTE because I thought it would be easier to visualize the code that you are generating.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Good luck.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 06 Nov 2013 19:15:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Converting-Char-to-Numeric/m-p/133495#M36247</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2013-11-06T19:15:38Z</dc:date>
    </item>
  </channel>
</rss>

