<?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: an easy way to convert numeric variable to character variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/an-easy-way-to-convert-numeric-variable-to-character-variable/m-p/131265#M26759</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi SASjedi and Haikuo,&lt;/P&gt;&lt;P&gt;Thank you for your reply! I use proc import and call execute to input the txt files. How should I tell sas to import the IDs as character variables?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 02 Jun 2012 12:09:08 GMT</pubDate>
    <dc:creator>HG</dc:creator>
    <dc:date>2012-06-02T12:09:08Z</dc:date>
    <item>
      <title>an easy way to convert numeric variable to character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/an-easy-way-to-convert-numeric-variable-to-character-variable/m-p/131262#M26756</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi community,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I need to import a lot of txt files. The variable ID in sas datasets can be either character or numeric.&amp;nbsp; When I set the sas datasets together I have to change the numeric ID to character ID. What is the efficient way to do it?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks! &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 01 Jun 2012 19:23:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/an-easy-way-to-convert-numeric-variable-to-character-variable/m-p/131262#M26756</guid>
      <dc:creator>HG</dc:creator>
      <dc:date>2012-06-01T19:23:52Z</dc:date>
    </item>
    <item>
      <title>Re: an easy way to convert numeric variable to character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/an-easy-way-to-convert-numeric-variable-to-character-variable/m-p/131263#M26757</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Use explicit conversion with a PUT or INPUT function.&amp;nbsp; For example:&lt;/P&gt;&lt;PRE lang="SAS"&gt;/* Data set ONE has numeric ID */
data one;
&amp;nbsp;&amp;nbsp; do id=1 to 3;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; T='One';
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;output;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end;
run;

/* Data set TWO has character ID */
data two;
&amp;nbsp;&amp;nbsp; do id='4','5','6';
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; T='Two';
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;output;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end;
run;

/* Concatenate ONE and TWO, make all ID's numeric */ 
Data ALL;
&amp;nbsp; set one
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; two (rename=(ID=xID));
&amp;nbsp; IF not missing(xID) then ID=INPUT(xID,5.);
&amp;nbsp; drop x:;
run;
&lt;/PRE&gt;&lt;P&gt;Results:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; id&amp;nbsp; T&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; --------------&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&amp;nbsp; One&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2&amp;nbsp; One&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 3&amp;nbsp; One&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 4&amp;nbsp; Two&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 5&amp;nbsp; Two&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 6&amp;nbsp; Two&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 02 Jun 2012 02:59:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/an-easy-way-to-convert-numeric-variable-to-character-variable/m-p/131263#M26757</guid>
      <dc:creator>SASJedi</dc:creator>
      <dc:date>2012-06-02T02:59:58Z</dc:date>
    </item>
    <item>
      <title>Re: an easy way to convert numeric variable to character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/an-easy-way-to-convert-numeric-variable-to-character-variable/m-p/131264#M26758</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;My suggestion kicks in before where you import txt files. 'ID' variable should always be character variable, as no matter what they look like, you can't do arithmetic operation on them anyway. Therefore, always import them as they are character variables.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Haikuo&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 02 Jun 2012 10:53:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/an-easy-way-to-convert-numeric-variable-to-character-variable/m-p/131264#M26758</guid>
      <dc:creator>Haikuo</dc:creator>
      <dc:date>2012-06-02T10:53:17Z</dc:date>
    </item>
    <item>
      <title>Re: an easy way to convert numeric variable to character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/an-easy-way-to-convert-numeric-variable-to-character-variable/m-p/131265#M26759</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi SASjedi and Haikuo,&lt;/P&gt;&lt;P&gt;Thank you for your reply! I use proc import and call execute to input the txt files. How should I tell sas to import the IDs as character variables?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 02 Jun 2012 12:09:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/an-easy-way-to-convert-numeric-variable-to-character-variable/m-p/131265#M26759</guid>
      <dc:creator>HG</dc:creator>
      <dc:date>2012-06-02T12:09:08Z</dc:date>
    </item>
    <item>
      <title>Re: an easy way to convert numeric variable to character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/an-easy-way-to-convert-numeric-variable-to-character-variable/m-p/131266#M26760</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi HG,&lt;/P&gt;&lt;P&gt;You can't tell proc import what variable type , it decides on its own. Call execute is used to execute a macro, I can't tell you what it does without viewing your code.&lt;/P&gt;&lt;P&gt;use&amp;nbsp; 'filename + data step + infile + input' to have a better control on your importing process.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Haikuo&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 02 Jun 2012 12:54:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/an-easy-way-to-convert-numeric-variable-to-character-variable/m-p/131266#M26760</guid>
      <dc:creator>Haikuo</dc:creator>
      <dc:date>2012-06-02T12:54:30Z</dc:date>
    </item>
    <item>
      <title>Re: an easy way to convert numeric variable to character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/an-easy-way-to-convert-numeric-variable-to-character-variable/m-p/131267#M26761</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I saved two files x1 and x2 at c:\temp\forum. &lt;/P&gt;&lt;P&gt;&amp;nbsp; x1 &lt;/P&gt;&lt;P&gt;id age&lt;/P&gt;&lt;P&gt;1 20&lt;/P&gt;&lt;P&gt;2 30&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; x2&lt;/P&gt;&lt;P&gt;id age&lt;/P&gt;&lt;P&gt;a 20&lt;/P&gt;&lt;P&gt;b 30&lt;/P&gt;&lt;P&gt;c 40&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;below is my code: &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;%let location=c:\temp\forum\;&lt;/P&gt;&lt;P&gt;filename mydate pipe "dir &amp;amp;location\*.txt/b";&lt;/P&gt;&lt;P&gt;data _null_;&lt;/P&gt;&lt;P&gt;&amp;nbsp; infile mydate truncover;&lt;/P&gt;&lt;P&gt;&amp;nbsp; length fname _fname $30;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; input fname;&lt;/P&gt;&lt;P&gt;&amp;nbsp; _fname=cats('_',scan(fname,1));&lt;/P&gt;&lt;P&gt;&amp;nbsp; if upcase(first(fname))='X' then &lt;/P&gt;&lt;P&gt;&amp;nbsp; call execute("proc import out=work."||_fname||&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; "&amp;nbsp; datafile="||"'&amp;amp;location."||fname||"'&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; DBMS=TAB REPLACE;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; GETNAMES=YES;&lt;/P&gt;&lt;P&gt;&amp;nbsp; delimiter='09'x;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; DATAROW=2;run; &lt;/P&gt;&lt;P&gt;&amp;nbsp; data "||_fname|| ";length nid $ 30; set "||_fname||";nid=compress(put(id,20.));drop id;run;");&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; data want (rename=(nid=id));&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; set _: indsname=ll;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; dsn=ll;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Linlin&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 03 Jun 2012 01:06:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/an-easy-way-to-convert-numeric-variable-to-character-variable/m-p/131267#M26761</guid>
      <dc:creator>Linlin</dc:creator>
      <dc:date>2012-06-03T01:06:15Z</dc:date>
    </item>
    <item>
      <title>Re: an easy way to convert numeric variable to character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/an-easy-way-to-convert-numeric-variable-to-character-variable/m-p/131268#M26762</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Agree with others that using a data step with infile/input gives you the control you need.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;One way of reducing the typing for such a data step is to use EG's import wizard with one of your text files. That creates you the full data step code. Then copy the generated code and amend it to your needs so that it works with all your text files with the same structure.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 03 Jun 2012 02:17:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/an-easy-way-to-convert-numeric-variable-to-character-variable/m-p/131268#M26762</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2012-06-03T02:17:02Z</dc:date>
    </item>
    <item>
      <title>Re: an easy way to convert numeric variable to character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/an-easy-way-to-convert-numeric-variable-to-character-variable/m-p/131269#M26763</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I like Patrick's answer. Import data manually using File Import.... and then copy the code from the log. Paste it into Sas Editor and change from charater to numeric or vice-versa. eg&lt;/P&gt;&lt;P&gt;I imported a file where postcode was numeric and I wanted it to be character. Whereever postcode appeared in the code copied from the log I changed it to $4.&lt;/P&gt;&lt;P&gt;Data xx ;&lt;/P&gt;&lt;P&gt;paste the code from log&lt;/P&gt;&lt;P&gt;change bits you want (per expalnation above) ; &lt;/P&gt;&lt;P&gt;Run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 07 Jun 2012 04:42:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/an-easy-way-to-convert-numeric-variable-to-character-variable/m-p/131269#M26763</guid>
      <dc:creator>Hen</dc:creator>
      <dc:date>2012-06-07T04:42:09Z</dc:date>
    </item>
    <item>
      <title>Re: an easy way to convert numeric variable to character variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/an-easy-way-to-convert-numeric-variable-to-character-variable/m-p/131270#M26764</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Hen,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;If you are using Base SAS then no need to paste the code from log,you can try importing file first and then just press F4 and you&amp;nbsp; can see all the code in the sas editor.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Shiva&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 07 Jun 2012 06:49:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/an-easy-way-to-convert-numeric-variable-to-character-variable/m-p/131270#M26764</guid>
      <dc:creator>shivas</dc:creator>
      <dc:date>2012-06-07T06:49:37Z</dc:date>
    </item>
  </channel>
</rss>

