<?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: check var type and convert if needed in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/check-var-type-and-convert-if-needed/m-p/820904#M324059</link>
    <description>&lt;P&gt;The error message is clear.&amp;nbsp; You tried to use a numeric format with a character variable.&lt;/P&gt;
&lt;P&gt;But you also have a logic problem with your code.&lt;/P&gt;
&lt;P&gt;The compiler checks all of the code for syntax errors, even the code that might never be executed.&lt;/P&gt;
&lt;P&gt;Since you never defined Y_NEW the compiler will use the first place it sees it being used to GUESS how you want it defined. Since the first place is the assignment of the value of the PUT() function then Y_NEW will always be a CHARACTER variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In general you should be doing this in two steps. One to determine the variable type. Then conditionally generate the code based on the type.&amp;nbsp; For example by creating a macro variable and using it to generate the code in the second step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But for the simple case of converting both CHAR and NUM variables to CHAR you can just write code that doesn't care what type the original variable was. Just use the CATS() function.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  length new_y $50;
  new_y=cats(y);
  rename new_y=y y=old_y;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note there is a side effect hat any leading spaces in a character variable Y will be removed.&lt;/P&gt;</description>
    <pubDate>Wed, 29 Jun 2022 13:33:49 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2022-06-29T13:33:49Z</dc:date>
    <item>
      <title>check var type and convert if needed</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-var-type-and-convert-if-needed/m-p/820899#M324055</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;I want to check type of var and if it numeric then convert to char and if it char then don't change it.&lt;/P&gt;
&lt;P&gt;Please see 2 examples.&lt;/P&gt;
&lt;P&gt;In example 2 there is an error,&lt;/P&gt;
&lt;P&gt;what is the way to solve it please?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/**Example1-Work well**/
Data ttt1;
 input x $ y ;
 cards;
 a 10
 b 20
 ;
 run;
Data want1;
set ttt1;
type_Y=vtype(Y);
IF type_Y='N' then Y_new=put(Y,best.);
else Y_new=Y;
Run;



/**Example2-Doesnt Work well**/
/*37         IF type_Y='N' then Y_new=put(Y,best.);*/
/*                                          _____*/
/*                                          484*/
 Data ttt2;
 input x $  y  $;
 cards;
 a 10
 b 20
 ;
 run;
 Data want2;
set ttt2;
type_Y=vtype(Y);
IF type_Y='N' then Y_new=put(Y,best.);
else Y_new=Y;
Run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 29 Jun 2022 12:37:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-var-type-and-convert-if-needed/m-p/820899#M324055</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2022-06-29T12:37:19Z</dc:date>
    </item>
    <item>
      <title>Re: check var type and convert if needed</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-var-type-and-convert-if-needed/m-p/820901#M324056</link>
      <description>&lt;P&gt;You PUT a &lt;U&gt;character&lt;/U&gt; variable, so the &lt;U&gt;numeric&lt;/U&gt; BEST format cannot be used.&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jun 2022 12:59:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-var-type-and-convert-if-needed/m-p/820901#M324056</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-06-29T12:59:19Z</dc:date>
    </item>
    <item>
      <title>Re: check var type and convert if needed</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-var-type-and-convert-if-needed/m-p/820904#M324059</link>
      <description>&lt;P&gt;The error message is clear.&amp;nbsp; You tried to use a numeric format with a character variable.&lt;/P&gt;
&lt;P&gt;But you also have a logic problem with your code.&lt;/P&gt;
&lt;P&gt;The compiler checks all of the code for syntax errors, even the code that might never be executed.&lt;/P&gt;
&lt;P&gt;Since you never defined Y_NEW the compiler will use the first place it sees it being used to GUESS how you want it defined. Since the first place is the assignment of the value of the PUT() function then Y_NEW will always be a CHARACTER variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In general you should be doing this in two steps. One to determine the variable type. Then conditionally generate the code based on the type.&amp;nbsp; For example by creating a macro variable and using it to generate the code in the second step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But for the simple case of converting both CHAR and NUM variables to CHAR you can just write code that doesn't care what type the original variable was. Just use the CATS() function.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  length new_y $50;
  new_y=cats(y);
  rename new_y=y y=old_y;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note there is a side effect hat any leading spaces in a character variable Y will be removed.&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jun 2022 13:33:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-var-type-and-convert-if-needed/m-p/820904#M324059</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-06-29T13:33:49Z</dc:date>
    </item>
    <item>
      <title>CATS,CATX functions with numeric inputs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-var-type-and-convert-if-needed/m-p/820924#M324064</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;As I understand&amp;nbsp; CATS,CATX are char functions?&lt;/P&gt;
&lt;P&gt;As I understand It means that the outcome of these functions is a char variable.&lt;/P&gt;
&lt;P&gt;I was sure that the input of these functions must be char too.&lt;/P&gt;
&lt;P&gt;In other question in this forum it was told me that I can also use numeric variables (as inputs of CATS,CATX functions ).&lt;/P&gt;
&lt;P&gt;May anyone explain it and verify that can use numeric variables?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jun 2022 14:15:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-var-type-and-convert-if-needed/m-p/820924#M324064</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2022-06-29T14:15:30Z</dc:date>
    </item>
    <item>
      <title>Re: CATS,CATX functions with numeric inputs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-var-type-and-convert-if-needed/m-p/820925#M324065</link>
      <description>&lt;P&gt;CATS and CATX can have numeric values as input. You can verify it yourself.&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jun 2022 14:21:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-var-type-and-convert-if-needed/m-p/820925#M324065</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-06-29T14:21:36Z</dc:date>
    </item>
    <item>
      <title>Re: CATS,CATX functions with numeric inputs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-var-type-and-convert-if-needed/m-p/820927#M324066</link>
      <description>&lt;P&gt;What about compress? Can it also get input numeric var?&lt;/P&gt;
&lt;P&gt;Can&amp;nbsp; all char functions get input numeric values?&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jun 2022 14:22:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-var-type-and-convert-if-needed/m-p/820927#M324066</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2022-06-29T14:22:43Z</dc:date>
    </item>
    <item>
      <title>Re: CATS,CATX functions with numeric inputs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-var-type-and-convert-if-needed/m-p/820929#M324067</link>
      <description>&lt;P&gt;The SAS documentation spells all this out.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV id="p1dxz68o1dz35yn12m6s6k64nidl" class="xisDoc-syntaxSimple"&gt;
&lt;DIV class="xisDoc-syntaxLevel"&gt;&lt;SPAN class="xisDoc-keyword"&gt;CATS&lt;/SPAN&gt;(&lt;EM class="xisDoc-userSuppliedValue"&gt;&lt;A tabindex="0" href="https://documentation.sas.com/doc/en/pgmmvacdc/9.4/lefunctionsref/n1e21rr6al5m2nn19r1fat5qxwrt.htm#p1sh72pnggy39rn1r8nqoa4hy1iu" target="_blank"&gt;item-1&lt;/A&gt;&lt;/EM&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN class="xisDoc-optional"&gt;&amp;lt;, …,&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;EM class="xisDoc-userSuppliedValue"&gt;&lt;A tabindex="0" href="https://documentation.sas.com/doc/en/pgmmvacdc/9.4/lefunctionsref/n1e21rr6al5m2nn19r1fat5qxwrt.htm#p1sh72pnggy39rn1r8nqoa4hy1iu" target="_blank"&gt;item-n&lt;/A&gt;&lt;/EM&gt;&amp;gt;&lt;/SPAN&gt;)&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;DIV class="xisDoc-syntaxDescription"&gt;
&lt;DIV class="xisDoc-requiredArgGroup"&gt;
&lt;H3 id="p1knxwc685exsqn1vnzya0c0aae4" class="xisDoc-title"&gt;Required Argument&lt;/H3&gt;
&lt;DIV id="p1sh72pnggy39rn1r8nqoa4hy1iu" class="xisDoc-argDescriptionPair"&gt;
&lt;H4 class="xisDoc-argument"&gt;&lt;EM class="xisDoc-userSuppliedValue"&gt;item&lt;/EM&gt;&lt;/H4&gt;
&lt;DIV class="xisDoc-argumentDescription"&gt;
&lt;P class="xisDoc-paraSimpleFirst"&gt;specifies a constant, variable, or expression,&lt;FONT color="#FF0000"&gt; either character or numeric&lt;/FONT&gt;. If&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;EM class="xisDoc-userSuppliedValue"&gt;item&lt;/EM&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;is numeric, its value is converted to a character string by using the BEST&lt;EM class="xisDoc-userSuppliedValue"&gt;w&lt;/EM&gt;. format. In this case, SAS does not write a note to the log&lt;/P&gt;
&lt;P class="xisDoc-paraSimpleFirst"&gt;&amp;nbsp;&lt;/P&gt;
&lt;DIV id="n1cop1e8j57m8in1exaob09cljtq" class="xisDoc-syntaxSimple"&gt;
&lt;DIV class="xisDoc-syntaxLevel"&gt;&lt;SPAN class="xisDoc-keyword"&gt;COMPRESS&lt;/SPAN&gt;(&lt;EM class="xisDoc-userSuppliedValue"&gt;&lt;A tabindex="0" href="https://documentation.sas.com/doc/en/pgmmvacdc/9.4/lefunctionsref/n0fcshr0ir3h73n1b845c4aq58hz.htm#p1cfydc2ifh484n1fodgztvpebog" target="_blank"&gt;source&lt;/A&gt;&lt;/EM&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;,&lt;SPAN class="xisDoc-optional"&gt;&amp;lt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;EM class="xisDoc-userSuppliedValue"&gt;&lt;A tabindex="0" href="https://documentation.sas.com/doc/en/pgmmvacdc/9.4/lefunctionsref/n0fcshr0ir3h73n1b845c4aq58hz.htm#p1cm6en3ihcusmn1u6x1ue6vyvcj" target="_blank"&gt;characters&lt;/A&gt;&lt;/EM&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;,&lt;SPAN class="xisDoc-optional"&gt;&amp;lt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;EM class="xisDoc-userSuppliedValue"&gt;&lt;A tabindex="0" href="https://documentation.sas.com/doc/en/pgmmvacdc/9.4/lefunctionsref/n0fcshr0ir3h73n1b845c4aq58hz.htm#p0t37skg8o4cfgn166syt5boeofr" target="_blank"&gt;modifier(s)&lt;/A&gt;&lt;/EM&gt;&amp;gt;&lt;/SPAN&gt;)&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;DIV class="xisDoc-syntaxDescription"&gt;
&lt;DIV class="xisDoc-requiredArgGroup"&gt;
&lt;H3 id="n1qew9s0x3dlhin1xcyrd0gcrecy" class="xisDoc-title"&gt;Required Argument&lt;/H3&gt;
&lt;DIV id="p1cfydc2ifh484n1fodgztvpebog" class="xisDoc-argDescriptionPair"&gt;
&lt;H4 class="xisDoc-argument"&gt;&lt;EM class="xisDoc-userSuppliedValue"&gt;source&lt;/EM&gt;&lt;/H4&gt;
&lt;DIV class="xisDoc-argumentDescription"&gt;
&lt;P class="xisDoc-paraSimpleFirst"&gt;specifies a &lt;FONT color="#FF0000"&gt;character&lt;/FONT&gt; constant, variable, or expression from which specified characters are removed.&lt;/P&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;</description>
      <pubDate>Wed, 29 Jun 2022 14:28:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-var-type-and-convert-if-needed/m-p/820929#M324067</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-06-29T14:28:56Z</dc:date>
    </item>
    <item>
      <title>Re: CATS,CATX functions with numeric inputs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-var-type-and-convert-if-needed/m-p/820931#M324068</link>
      <description>&lt;P&gt;Because SAS (the data step compiler) will automatically convert types of values wherever needed, functions that expect character values will work with numeric variables, but often with unexpected and sometime hilarious effects. Serious SAS users will not let that happen and do the conversion explicitly.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The deeper question here: why is there a need to convert data types in the first place? Instead of repairing issues after the fact, you need to look at the process where the variables originate, and do the fix there.&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jun 2022 14:31:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-var-type-and-convert-if-needed/m-p/820931#M324068</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-06-29T14:31:38Z</dc:date>
    </item>
    <item>
      <title>Re: CATS,CATX functions with numeric inputs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-var-type-and-convert-if-needed/m-p/820933#M324070</link>
      <description>&lt;P&gt;You need to be aware that any function that will create a character result from a numeric value will use SAS default rules if you do not take control of the conversion. Control usually means a format and possibly a length or justification.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Many of the conversions will use a BEST12 format resulting in leading spaces, or in the case of "large" values exponential notation and/or truncation to fit the number of characters.&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jun 2022 14:36:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-var-type-and-convert-if-needed/m-p/820933#M324070</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-06-29T14:36:46Z</dc:date>
    </item>
    <item>
      <title>Re: check var type and convert if needed</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-var-type-and-convert-if-needed/m-p/821030#M324121</link>
      <description>&lt;P&gt;Here's a workaround to a tricky problem:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data _null_;
set ttt2;
call execute('data want2; set tt2;');
type_Y=vtype(Y);
if type_Y='N' then call execute('Y_new=put(Y,best.);');
else call execute('Y_new=Y;');
call execute('run;');
stop;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It's not clear that "best." is a good format choice.&amp;nbsp; If that turns out to be the case, you will need to inspect the results, and perhaps post another question.&lt;/P&gt;</description>
      <pubDate>Wed, 29 Jun 2022 21:18:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-var-type-and-convert-if-needed/m-p/821030#M324121</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2022-06-29T21:18:20Z</dc:date>
    </item>
    <item>
      <title>Re: CATS,CATX functions with numeric inputs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-var-type-and-convert-if-needed/m-p/821064#M324144</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;What about compress? Can it also get input numeric var?&lt;/P&gt;
&lt;P&gt;Can&amp;nbsp; all char functions get input numeric values?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You can easily find that out yourself. Maxim 4 says&lt;/P&gt;
&lt;H4 id="toc-hId--1952525832" class="maxim-western"&gt;If in doubt, do a test run and look at the results. If puzzled, inquire further.&lt;/H4&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So run this, and study the log closely:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
x1 = 12345;
x2 = compress(x1);
put x2=;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 30 Jun 2022 07:01:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-var-type-and-convert-if-needed/m-p/821064#M324144</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-06-30T07:01:17Z</dc:date>
    </item>
    <item>
      <title>Re: check var type and convert if needed</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-var-type-and-convert-if-needed/m-p/821115#M324165</link>
      <description>Use  " Y_new=vvalue(Y) " instead of "Y_new=put(Y,best.)"</description>
      <pubDate>Thu, 30 Jun 2022 12:25:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-var-type-and-convert-if-needed/m-p/821115#M324165</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-06-30T12:25:21Z</dc:date>
    </item>
    <item>
      <title>Re: check var type and convert if needed</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-var-type-and-convert-if-needed/m-p/821125#M324169</link>
      <description>Hello&lt;BR /&gt;What vvalue is doing?</description>
      <pubDate>Thu, 30 Jun 2022 13:06:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-var-type-and-convert-if-needed/m-p/821125#M324169</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2022-06-30T13:06:00Z</dc:date>
    </item>
    <item>
      <title>Re: check var type and convert if needed</title>
      <link>https://communities.sas.com/t5/SAS-Programming/check-var-type-and-convert-if-needed/m-p/821127#M324171</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Hello&lt;BR /&gt;What vvalue is doing?&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Read the documentation&amp;nbsp;&lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.2/lefunctionsref/p06uh7sqlh94nxn1gezyms3e9njn.htm" target="_self"&gt;vvalue()&lt;/A&gt;&amp;nbsp;or &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.2/lefunctionsref/n1x70jnnpttwy0n1gf5rvuivgyry.htm" target="_self"&gt;vvaluex()&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 30 Jun 2022 13:14:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/check-var-type-and-convert-if-needed/m-p/821127#M324171</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-06-30T13:14:48Z</dc:date>
    </item>
  </channel>
</rss>

