<?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 How to use cast function in SAS in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-cast-function-in-SAS/m-p/825885#M326210</link>
    <description>&lt;P&gt;I have SQL Server queries which needs to be convert into SAS code. So, I am writing the queries under PROC SQL in SAS and using the cast function in SELECT CLAUSE, but I am getting error at place 'as'. PFB, the example:-&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;&amp;nbsp;select cast(variable_name as nvarchar) as Variable1 from table1;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;I am getting the error at first 'as' place and showing me the syntax error.&lt;/P&gt;</description>
    <pubDate>Thu, 28 Jul 2022 11:16:30 GMT</pubDate>
    <dc:creator>dewanganrahul</dc:creator>
    <dc:date>2022-07-28T11:16:30Z</dc:date>
    <item>
      <title>How to use cast function in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-cast-function-in-SAS/m-p/825885#M326210</link>
      <description>&lt;P&gt;I have SQL Server queries which needs to be convert into SAS code. So, I am writing the queries under PROC SQL in SAS and using the cast function in SELECT CLAUSE, but I am getting error at place 'as'. PFB, the example:-&lt;/P&gt;&lt;P&gt;proc sql;&lt;/P&gt;&lt;P&gt;&amp;nbsp;select cast(variable_name as nvarchar) as Variable1 from table1;&lt;/P&gt;&lt;P&gt;quit;&lt;/P&gt;&lt;P&gt;I am getting the error at first 'as' place and showing me the syntax error.&lt;/P&gt;</description>
      <pubDate>Thu, 28 Jul 2022 11:16:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-cast-function-in-SAS/m-p/825885#M326210</guid>
      <dc:creator>dewanganrahul</dc:creator>
      <dc:date>2022-07-28T11:16:30Z</dc:date>
    </item>
    <item>
      <title>Re: How to use cast function in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-cast-function-in-SAS/m-p/825896#M326218</link>
      <description>&lt;P&gt;SAS Proc SQL does not support CAST function. You have to use put function to convert a numeric value to a character AND input function to convert a character value to a numeric.&lt;BR /&gt;CAST&amp;nbsp; is however&amp;nbsp; available&amp;nbsp; in Federated SQL in SAS.&lt;/P&gt;</description>
      <pubDate>Thu, 28 Jul 2022 11:52:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-cast-function-in-SAS/m-p/825896#M326218</guid>
      <dc:creator>Sajid01</dc:creator>
      <dc:date>2022-07-28T11:52:36Z</dc:date>
    </item>
    <item>
      <title>Re: How to use cast function in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-cast-function-in-SAS/m-p/825903#M326222</link>
      <description>&lt;P&gt;Good day&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;An alternative approach to your query can be written as follow. I've also included conversion from character to numeric, and numeric to character.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;/*Dummy data*/
data have;
	infile datalines;
	input _char $ _num;
	datalines;
A 504505
B 454505
C 554505
D 604505
E 404505
F 434505
;
run;

/*Convert character to numeric, or convert numeric to character*/
proc sql;
	create table want as
		select 
			_char format=$10. as Variable_CHAR
			,_num format=10. as Variable_NUM
			,input(_char,10.) format=10. as Variable_CHAR2NUM
			,put(_num,10.) format=$10. as Variable_NUM2CHAR
		from have;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 28 Jul 2022 12:20:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-cast-function-in-SAS/m-p/825903#M326222</guid>
      <dc:creator>Adriaan_Gouws</dc:creator>
      <dc:date>2022-07-28T12:20:52Z</dc:date>
    </item>
    <item>
      <title>Re: How to use cast function in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-cast-function-in-SAS/m-p/825922#M326227</link>
      <description>&lt;P&gt;Why attach formats to the variables?&lt;/P&gt;
&lt;P&gt;The only places you might need to do something is when you need VARIABLE_CHAR to be a different LENGTH than _CHAR.&amp;nbsp; Attaching a format that only displays the first 10 bytes is different than telling SAS to only store 10 bytes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;There could be value in attaching the 10. format specification to VARIABLE_NUM or VARIABLE_CHAR2NUM, but in most situations SAS will use BEST12. format to display numbers, so that should work fine for 10 digit integers.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;_char length=10 as Variable_CHAR
,_num as Variable_NUM
,input(_char,10.) as Variable_CHAR2NUM
,put(_num,10.) as Variable_NUM2CHAR&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 28 Jul 2022 13:53:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-cast-function-in-SAS/m-p/825922#M326227</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-07-28T13:53:30Z</dc:date>
    </item>
    <item>
      <title>Re: How to use cast function in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-cast-function-in-SAS/m-p/826245#M326365</link>
      <description>&lt;P&gt;Why do you need to convert your SQL Server SQL queries to SAS SQL? If you still want to keep running your queries on SQL Server from SAS, just use SQL Passthru and don't change your SQL at all (CAST etc will work fine from SAS). If you have a lot of code to convert this will save a lot of effort.&lt;/P&gt;</description>
      <pubDate>Fri, 29 Jul 2022 23:57:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-cast-function-in-SAS/m-p/826245#M326365</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2022-07-29T23:57:14Z</dc:date>
    </item>
    <item>
      <title>Re: How to use cast function in SAS</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-cast-function-in-SAS/m-p/897997#M354934</link>
      <description>&lt;P&gt;SAS datasets support only fixed-width character or double-precision floating-point numeric data. The DATA step and most base SAS procedures can only process those two data types. Databases typically support various additional ANSI data types, including DECIMAL(NUMERIC), VARCHAR, INT, BIGINT, and more. In base SAS FedSQL and DS2 can process these extra data types, but PROC SQL cannot. It is important to remember that, even if you successfully process VARCHAR or DECIMAL data in SAS, you must write the results to a database table to preserve those data types. Saving the result as a SAS data set automatically converts all data types to fixed-width character or double-precision floating-point numeric data types. For this discussion, I'll use these two tables, one a SAS data set, and the other an Oracle table:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="SASJedi_0-1696960421615.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/88746i0949D3252BAF4948/image-size/medium?v=v2&amp;amp;px=400" role="button" title="SASJedi_0-1696960421615.png" alt="SASJedi_0-1696960421615.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;To resolve an SQL expression, all operands must be the same data type. Automatic data type conversion is not supported in SQL.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select fractional+numtext as Total
   from sas.myTable;
quit;

proc fedsql;
select fractional+numtext as Total
   from sas.myTable;
quit
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Log:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select fractional+numtext as Total
   from sas.myTable;
&lt;FONT color="#FF0000"&gt;ERROR: Expression using addition (+) requires numeric types.
NOTE: PROC SQL set option NOEXEC and will continue to check the syntax of statements.&lt;/FONT&gt;
…
proc fedsql;
select fractional+numtext as Total
   from sas.myTable;
&lt;FONT color="#FF0000"&gt;ERROR: Operator does not exist:  DOUBLE + CHAR
ERROR: No operator matches the given name and argument type(s). You might need to add explicit typecasts.
&lt;/FONT&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;In FedSQL and native database SQL, you can explicitly convert data types using the CAST function. As noted by &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/131732"&gt;@Sajid01&lt;/a&gt;, PROC SQL does not support the CAST function but instead relies on the base SAS PUT and INPUT functions for data type conversion.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;title "PROC SQL Results";
proc sql;
select sum(fractional+input(numtext,32.)) as Total
   from db.myTable;
quit;

title "PROC FedSQL Results";
proc fedsql iptrace;
select sum(fractional+cast(numtext as double)) as Total
   from db.myTable;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="SASJedi_1-1696960640843.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/88747iF32CFBD740F6C947/image-size/medium?v=v2&amp;amp;px=400" role="button" title="SASJedi_1-1696960640843.png" alt="SASJedi_1-1696960640843.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;The advantage of using PROC FedSQL in this case is that the SQL processing can be pushed into the database, minimizing data movement. Because PROC SQL uses non-ANSI code to convert the data type, the processing must be done in SAS.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
select fractional+input(numtext,32.) as Total
   from db.myTable;
&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;SAS_SQL:  Unable to convert the query to a DBMS specific SQL statement due to an error. 
ACCESS ENGINE:  SQL statement was not passed to the DBMS, SAS will do the processing.&lt;/STRONG&gt; &lt;/FONT&gt;
ORACLE_24: Prepared: on connection 0
SELECT  "FRACTIONAL", "NUMTEXT" FROM EDU.MYTABLE 
ORACLE_25: Executed: on connection 0
SELECT statement  ORACLE_24
…
proc fedsql iptrace;
select fractional+cast(numtext as double) as Total
   from db.myTable;
&lt;FONT color="#0000FF"&gt;IPTRACE: FULL pushdown to ORACLE SUCCESS!
IPTRACE: Retextualized child query:&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;select SUM (("EDU"."MYTABLE"."FRACTIONAL"+
      cast("EDU"."MYTABLE"."NUMTEXT" as DOUBLE PRECISION))) as "TOTAL" 
   from "EDU"."MYTABLE"&lt;/FONT&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note that PROC SQL had to bring the data to SAS to do the type conversion and summarization, but by using the CAST function, PROC FedSQL manage to get full push-down to the database.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 10 Oct 2023 18:00:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-cast-function-in-SAS/m-p/897997#M354934</guid>
      <dc:creator>SASJedi</dc:creator>
      <dc:date>2023-10-10T18:00:36Z</dc:date>
    </item>
  </channel>
</rss>

