Change Field Data Type from Numeric to Character in PROC SQL

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 5
Accepted Solution

Change Field Data Type from Numeric to Character in PROC SQL

I need to change a data type for a field (field1) that I am querying from numeric to character. Here is the code that runs and provides the field back as a numeric data type using PROC SQL. I have been able to convert the number to a character using the Data statement along with the put function but can thus be accomplished in the PROC SQL statement?

proc sql;

connect to oracle as example(user=***** pw=***** path=abcd.xxxxx.com);

create table work.data as

select *

from connection to example

(

select field1, field2, from example.Address

where Address_Type = 'PRIMARY'

);

quit;

Here is what I have tried but get the following error message

proc sql;

connect to oracle as example (user=xxxxx pw=xxxx path=abcd.xxxxx.com.com);

create table work.data as

select *

from connection to example

(

select field1 as put(field1, $15.), field2 from example.Agency_Address

where Address_Type = 'PRIMARY'

);

quit;

ERROR: ORACLE prepare error: ORA-00923: FROM keyword not found where expected. SQL statement: select field1 as

       left(put(field1, $15.)), field2 from Example.Agency_Address where Address_Type = 'PRIMARY'.

Thanks in advance for your time.


Accepted Solutions
Solution
‎06-09-2015 02:10 PM
Valued Guide
Posts: 854

Re: Change Field Data Type from Numeric to Character in PROC SQL

The put function you are using will work the same way if you use proc sql;, what you are showing here is passing through to oracle, you will need to use a function that works with the database you are connecting too, not something that will work within SAS.

View solution in original post


All Replies
Solution
‎06-09-2015 02:10 PM
Valued Guide
Posts: 854

Re: Change Field Data Type from Numeric to Character in PROC SQL

The put function you are using will work the same way if you use proc sql;, what you are showing here is passing through to oracle, you will need to use a function that works with the database you are connecting too, not something that will work within SAS.

Respected Advisor
Posts: 4,973

Re: Change Field Data Type from Numeric to Character in PROC SQL

The PUT function always returns a character string.

The second parameter (in this case $15.) must use a dollar sign when the first parameter is character, and must omit the dollar sign when the first parameter is numeric.

Since you're making a numeric to character conversion, try removing the dollar sign:

put(field1, 15.)

Occasional Contributor
Posts: 13

Re: Change Field Data Type from Numeric to Character in PROC SQL

Since you are using pass through functionality you can put the transformation into either the SAS part of the statement or the Oracle part of the statement.

If you're going to place the transformation into the oracle statement, use oracle language, for instance TO_CHAR .

Esteemed Advisor
Posts: 6,646

Re: Change Field Data Type from Numeric to Character in PROC SQL

Do the conversion in SAS:

proc sql;

connect to oracle as example (user=xxxxx pw=xxxx path=abcd.xxxxx.com.com);

create table work.data as

select put(field1,15.) as field1, field2

from connection to example

(

select field1, field2 from example.Agency_Address

where Address_Type = 'PRIMARY'

);

quit;

Also note the reverse ordering of the select ... as ... (function first, name second)

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Esteemed Advisor
Esteemed Advisor
Posts: 7,203

Re: Change Field Data Type from Numeric to Character in PROC SQL

The above posters have given you the answer here, in pass-through code the syntax must match the Database syntax requirements.  In SAS SQL you can use SAS syntax rules.  Maybe if you want to combine the two (and of course, avoiding the * syntax totally, because that would just be guessing!):

proc sql;

     connect to oracle as example (user=xxxxx pw=xxxx path=abcd.xxxxx.com.com);

     create table work.data as

/* This part is processed by SAS and must conform to SAS syntax */

     select     put(FIELD1,best.) as FIELD1,

                     FIELD2 as FIELD2

     from connection to example

/* This part is passed to the database and must conform to the database syntax */

          (

               select field1, field2 from example.Agency_Address

               where Address_Type = 'PRIMARY'

          );

quit;

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 294 views
  • 0 likes
  • 6 in conversation