<?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 variable type then convert it to character in SAS Studio</title>
    <link>https://communities.sas.com/t5/SAS-Studio/Check-variable-type-then-convert-it-to-character/m-p/429870#M4406</link>
    <description>&lt;P&gt;You probably need to fix this issue earlier in your process.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If your input files are CSV files then write a DATA step to read them. Do not use PROC IMPORT because that will have to guess at how to define the variables, and the results could be inconsistent among different source files that contain differently formatted data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you can't be certain whether the data set you're working with will have a numeric or character version of a variable, then you should be able to use the VVALUE() function to get character version of your variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This data step will create a character version of OLDVAR and will work whether OLDVAR is currently character or numeric.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  set have ;
  length newvar $20 ;
  newvar=vvalue(oldvar);
  drop oldvar ;
  rename newvar=oldvar;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;From &lt;A href="https://go.documentation.sas.com/?docsetId=lefunctionsref&amp;amp;docsetTarget=p06uh7sqlh94nxn1gezyms3e9njn.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en" target="_self"&gt;the VVALUE documentation&lt;/A&gt;:&amp;nbsp; The PUT function enables you to reformat a specified variable or constant. VVALUE uses the current format that is associated with the variable.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 11 Apr 2019 20:07:51 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2019-04-11T20:07:51Z</dc:date>
    <item>
      <title>Check variable type then convert it to character</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Check-variable-type-then-convert-it-to-character/m-p/427603#M4320</link>
      <description>&lt;P&gt;Is there any way I could find out if a variable is character or numeric? If it's numeric then I need to convert it to character. If it's character then converting will not be needed.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried the if vartype(varname,18) = N then varname_1 = compress(put(varname,best12.)); else varname_1 = varname;&lt;/P&gt;&lt;P&gt;But it didn't work.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I will appreciate any suggestion! Thank you!&lt;/P&gt;</description>
      <pubDate>Mon, 15 Jan 2018 05:21:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Check-variable-type-then-convert-it-to-character/m-p/427603#M4320</guid>
      <dc:creator>leela214</dc:creator>
      <dc:date>2018-01-15T05:21:03Z</dc:date>
    </item>
    <item>
      <title>Re: Check variable type then convert it to character</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Check-variable-type-then-convert-it-to-character/m-p/427604#M4321</link>
      <description>&lt;P&gt;it is not vartype, it is vtype.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Try in a datastep with if _n_=1 and check and then do&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if vtype(variable)='N' then do; /*your conversion*/&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;or &lt;STRONG&gt;vartype&lt;/STRONG&gt;, you can use it in macro language as explained in documentation&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000148443.htm" target="_blank"&gt;http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000148443.htm&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 15 Jan 2018 05:32:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Check-variable-type-then-convert-it-to-character/m-p/427604#M4321</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-01-15T05:32:30Z</dc:date>
    </item>
    <item>
      <title>Re: Check variable type then convert it to character</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Check-variable-type-then-convert-it-to-character/m-p/427612#M4322</link>
      <description>&lt;P&gt;You should not need to find that out. You should &lt;EM&gt;know your data&lt;/EM&gt; (see Maxim 3) from the beginning (when you retrieve data from external sources or create it). From there, it's just following the documentation.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One way to really convert (quasi in place) is this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
x1 = '2';
run;

%macro convert_to_num(varname,library,dataset);

proc sql noprint;
select type into :vartype
from dictionary.columns
where libname = upcase("&amp;amp;library.") and memname = upcase("&amp;amp;dataset.") and upcase(name) = upcase("&amp;amp;varname.")
;
quit;

%if &amp;amp;vartype. = char
%then %do;

data &amp;amp;library..&amp;amp;dataset. (drop=__&amp;amp;varname.);
set &amp;amp;library..&amp;amp;dataset. (rename=(&amp;amp;varname.=__&amp;amp;varname.));
&amp;amp;varname. = input(__&amp;amp;varname.,best.);
run;

%end;

%mend;

%convert_to_num(x1,work,test)&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 15 Jan 2018 07:41:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Check-variable-type-then-convert-it-to-character/m-p/427612#M4322</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-01-15T07:41:24Z</dc:date>
    </item>
    <item>
      <title>Re: Check variable type then convert it to character</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Check-variable-type-then-convert-it-to-character/m-p/427621#M4323</link>
      <description>&lt;P&gt;Is this because the data is coming in from Excel by any chance?&amp;nbsp; That is the number one cause of garbage data.&amp;nbsp; Fix your input source (maybe use CSV), and write a datastep to read the data in per your import specification.&amp;nbsp; Removes this issue totally.&lt;/P&gt;</description>
      <pubDate>Mon, 15 Jan 2018 09:11:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Check-variable-type-then-convert-it-to-character/m-p/427621#M4323</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-01-15T09:11:24Z</dc:date>
    </item>
    <item>
      <title>Re: Check variable type then convert it to character</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Check-variable-type-then-convert-it-to-character/m-p/429866#M4403</link>
      <description>&lt;P&gt;I have tried using vtype but it didn't work as well.&lt;/P&gt;&lt;P&gt;I tried using vtype in the if then statement like you've kindly suggested but it didn't work. So, I created an variable to use in the if then statement, didn't work too... It would be nice if it did! Would be happy to know how I can fix this in order for it to work.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;random_no in here is character so it should go int to the else statement but it did't.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.PNG" style="width: 571px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/18034i45A45F853944C1BA/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture1.PNG" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/18035iB6CF3070237DAF84/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture1.PNG" alt="Capture1.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 23 Jan 2018 02:56:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Check-variable-type-then-convert-it-to-character/m-p/429866#M4403</guid>
      <dc:creator>leela214</dc:creator>
      <dc:date>2018-01-23T02:56:11Z</dc:date>
    </item>
    <item>
      <title>Re: Check variable type then convert it to character</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Check-variable-type-then-convert-it-to-character/m-p/429867#M4404</link>
      <description>The source is csv. But, the same variable in different dataset could be in different type. sometimes numeric, sometimes character. So, I want to add a step in my macro to check for variable type and convert it if necessary.</description>
      <pubDate>Tue, 23 Jan 2018 02:59:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Check-variable-type-then-convert-it-to-character/m-p/429867#M4404</guid>
      <dc:creator>leela214</dc:creator>
      <dc:date>2018-01-23T02:59:57Z</dc:date>
    </item>
    <item>
      <title>Re: Check variable type then convert it to character</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Check-variable-type-then-convert-it-to-character/m-p/429868#M4405</link>
      <description>the same variable in different datasets could be in different type. sometimes numeric, sometimes character. So, I want to add a step in my macro to check for variable type and convert it if necessary.&lt;BR /&gt;&lt;BR /&gt;your solution did help converting the variable but is there a simpler way?&lt;BR /&gt;&lt;BR /&gt;Thank you for your code! It did work!</description>
      <pubDate>Tue, 23 Jan 2018 03:04:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Check-variable-type-then-convert-it-to-character/m-p/429868#M4405</guid>
      <dc:creator>leela214</dc:creator>
      <dc:date>2018-01-23T03:04:20Z</dc:date>
    </item>
    <item>
      <title>Re: Check variable type then convert it to character</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Check-variable-type-then-convert-it-to-character/m-p/429870#M4406</link>
      <description>&lt;P&gt;You probably need to fix this issue earlier in your process.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If your input files are CSV files then write a DATA step to read them. Do not use PROC IMPORT because that will have to guess at how to define the variables, and the results could be inconsistent among different source files that contain differently formatted data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you can't be certain whether the data set you're working with will have a numeric or character version of a variable, then you should be able to use the VVALUE() function to get character version of your variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This data step will create a character version of OLDVAR and will work whether OLDVAR is currently character or numeric.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  set have ;
  length newvar $20 ;
  newvar=vvalue(oldvar);
  drop oldvar ;
  rename newvar=oldvar;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;From &lt;A href="https://go.documentation.sas.com/?docsetId=lefunctionsref&amp;amp;docsetTarget=p06uh7sqlh94nxn1gezyms3e9njn.htm&amp;amp;docsetVersion=9.4&amp;amp;locale=en" target="_self"&gt;the VVALUE documentation&lt;/A&gt;:&amp;nbsp; The PUT function enables you to reformat a specified variable or constant. VVALUE uses the current format that is associated with the variable.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 11 Apr 2019 20:07:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Check-variable-type-then-convert-it-to-character/m-p/429870#M4406</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-04-11T20:07:51Z</dc:date>
    </item>
    <item>
      <title>Re: Check variable type then convert it to character</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Check-variable-type-then-convert-it-to-character/m-p/429895#M4407</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/182361"&gt;@leela214&lt;/a&gt; wrote:&lt;BR /&gt;the same variable in different datasets could be in different type. sometimes numeric, sometimes character. So, I want to add a step in my macro to check for variable type and convert it if necessary.&lt;BR /&gt;&lt;BR /&gt;your solution did help converting the variable but is there a simpler way?&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Of course. Set the correct type and other attributes in the data step that reads the csv into SAS in the first place.&lt;/P&gt;</description>
      <pubDate>Tue, 23 Jan 2018 06:10:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Check-variable-type-then-convert-it-to-character/m-p/429895#M4407</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-01-23T06:10:47Z</dc:date>
    </item>
    <item>
      <title>Re: Check variable type then convert it to character</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Check-variable-type-then-convert-it-to-character/m-p/429913#M4409</link>
      <description>&lt;P&gt;This makes no sense.&amp;nbsp;&amp;nbsp;&lt;U&gt;&lt;STRONG&gt;You&lt;/STRONG&gt;&lt;/U&gt; are in charge of your data.&amp;nbsp; You import it it and set the formats and such like.&amp;nbsp; The reason you are having problems is because you are declining to do this important step.&amp;nbsp; Write a datastep, specify informats, formats, lengths and labels, make your data look the way you know it should, don't rely on guessing procedures like proc import.&amp;nbsp; This is the way you handle data correctly, documented and repeatable.&lt;/P&gt;</description>
      <pubDate>Tue, 23 Jan 2018 08:51:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Check-variable-type-then-convert-it-to-character/m-p/429913#M4409</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-01-23T08:51:49Z</dc:date>
    </item>
    <item>
      <title>Re: Check variable type then convert it to character</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Check-variable-type-then-convert-it-to-character/m-p/430267#M4431</link>
      <description>Thank you for letting me know that I need to set the formats. I am very new to sas so I wouldn't say I'm declining to do it. It's more like I didn't know/how so I just did what I do know instead. I was taught that proc import is easier and faster when I want to import data into sas. Thank you for taking the time to read my post!</description>
      <pubDate>Wed, 24 Jan 2018 03:21:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Check-variable-type-then-convert-it-to-character/m-p/430267#M4431</guid>
      <dc:creator>leela214</dc:creator>
      <dc:date>2018-01-24T03:21:37Z</dc:date>
    </item>
    <item>
      <title>Re: Check variable type then convert it to character</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Check-variable-type-then-convert-it-to-character/m-p/430268#M4432</link>
      <description>It worked! Thank you for this. Is there anything I need to watch out for or be careful of when I use the vvalue function? It's way too good to be truth. If this can convert numeric to char, why would we need put function for converting numeric to char?</description>
      <pubDate>Wed, 24 Jan 2018 03:24:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Check-variable-type-then-convert-it-to-character/m-p/430268#M4432</guid>
      <dc:creator>leela214</dc:creator>
      <dc:date>2018-01-24T03:24:42Z</dc:date>
    </item>
  </channel>
</rss>

