<?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: Applying format in SQL table in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Applying-format-in-SQL-table/m-p/588553#M168219</link>
    <description>Not really, either way you're creating a new variable. You could create a view that did the conversion and then upload the view to avoid changing names if that's your issue. &lt;BR /&gt;&lt;BR /&gt;</description>
    <pubDate>Fri, 13 Sep 2019 14:53:42 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2019-09-13T14:53:42Z</dc:date>
    <item>
      <title>Applying format in SQL table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Applying-format-in-SQL-table/m-p/588520#M168210</link>
      <description>&lt;P&gt;I'm unable to copy the user defined Format of one SAS variable to the SQL table. In the below example, 'metadata_final' is a SAS dataset where it holds the formatted value for the variable STAT_CD. However when I tried copy the SAS table which has that formatted variable in SQL table it is not copying the formatted values instead it is copying the actual value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Log is given below and even if&amp;nbsp;I apply the Format Statement before creating the SQL table, it is not taking the Format. No WARNING or ERROR message in the log as well.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;May I know how can I copy the formatted value in SQL table?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;27         data SQLLIB.SQL_table;&lt;BR /&gt;28         Format STAT_CD $STAT_CD.;
29         	set metadata_final;
30         	run_id=sum(n,1);
31         	output;
32         	stop;
33         	set SQLLIB.SQL_table nobs=n;
34         	modify SQLLIB.SQL_table;
35         run;

NOTE: There were 1 observations read from the data set WORK.METADATA_FINAL.
NOTE: The data set SQLLIB..SQL_TABLE has been updated.  There were 0 observations rewritten, 1 observations added and 0 observations 
      deleted.&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 13 Sep 2019 13:42:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Applying-format-in-SQL-table/m-p/588520#M168210</guid>
      <dc:creator>Babloo</dc:creator>
      <dc:date>2019-09-13T13:42:35Z</dc:date>
    </item>
    <item>
      <title>Re: Applying format in SQL table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Applying-format-in-SQL-table/m-p/588531#M168212</link>
      <description>&lt;P&gt;please try below code&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;   data SQLLIB.SQL_table;
   	set metadata_final;
   	run_id=sum(n,1);
   	output;
   	stop;
   	set SQLLIB.SQL_table nobs=n;
   	modify SQLLIB.SQL_table;
     Format STAT_CD $STAT_CD.;
   run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 13 Sep 2019 14:07:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Applying-format-in-SQL-table/m-p/588531#M168212</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2019-09-13T14:07:47Z</dc:date>
    </item>
    <item>
      <title>Re: Applying format in SQL table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Applying-format-in-SQL-table/m-p/588534#M168214</link>
      <description>&lt;P&gt;No, it's not working. I already tried placing the FORMAT Statement at different positions and it's not working.&lt;/P&gt;</description>
      <pubDate>Fri, 13 Sep 2019 14:11:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Applying-format-in-SQL-table/m-p/588534#M168214</guid>
      <dc:creator>Babloo</dc:creator>
      <dc:date>2019-09-13T14:11:35Z</dc:date>
    </item>
    <item>
      <title>Re: Applying format in SQL table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Applying-format-in-SQL-table/m-p/588535#M168215</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/8409"&gt;@Babloo&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I'm unable to copy the user defined Format of one SAS variable to the SQL table. In the below example, 'metadata_final' is a SAS dataset where it holds the formatted value for the variable STAT_CD. However when I tried copy the SAS table which has that formatted variable in SQL table it is not copying the formatted values instead it is copying the actual value.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;That the actual value is ALWAYS copied is the way SAS works. Always. If you want the "formatted value" as the actual value then you need to use something explict&amp;nbsp;like Put(variable,formatname.) as FormattedValue (SQL) or FormattedValue = put(variable,formatname.) (data step)&amp;nbsp;At which point the formatted value is not the actual value and often not the same type (numeric to character conversion with PUT) or length.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 13 Sep 2019 14:20:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Applying-format-in-SQL-table/m-p/588535#M168215</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-09-13T14:20:37Z</dc:date>
    </item>
    <item>
      <title>Re: Applying format in SQL table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Applying-format-in-SQL-table/m-p/588539#M168217</link>
      <description>Got it. Can we do this without creating the new variable while I do with&lt;BR /&gt;PUT function?&lt;BR /&gt;</description>
      <pubDate>Fri, 13 Sep 2019 14:32:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Applying-format-in-SQL-table/m-p/588539#M168217</guid>
      <dc:creator>Babloo</dc:creator>
      <dc:date>2019-09-13T14:32:44Z</dc:date>
    </item>
    <item>
      <title>Re: Applying format in SQL table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Applying-format-in-SQL-table/m-p/588553#M168219</link>
      <description>Not really, either way you're creating a new variable. You could create a view that did the conversion and then upload the view to avoid changing names if that's your issue. &lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Fri, 13 Sep 2019 14:53:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Applying-format-in-SQL-table/m-p/588553#M168219</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-09-13T14:53:42Z</dc:date>
    </item>
    <item>
      <title>Re: Applying format in SQL table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Applying-format-in-SQL-table/m-p/588612#M168246</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/8409"&gt;@Babloo&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Got it. Can we do this without creating the new variable while I do with&lt;BR /&gt;PUT function?&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;It would depend to some extent on the existing variable type and if&amp;nbsp;using a data step the existing defined length.&lt;/P&gt;
&lt;P&gt;In a data step changing types would fail.&lt;/P&gt;
&lt;P&gt;SQL is apparently less concerned so you can use the same variable name:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc format library=work;
value $someformat
"This is a test" ="Only a test"
;
data example;
   x = "This is a test";
   y = 12345;
run;

proc sql; 
   create table work.outformat as
   select Put(x,$someformat.) as x, put(y,comma9.) as y
   from example
   ;
quit;

&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 13 Sep 2019 17:46:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Applying-format-in-SQL-table/m-p/588612#M168246</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-09-13T17:46:34Z</dc:date>
    </item>
    <item>
      <title>Re: Applying format in SQL table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Applying-format-in-SQL-table/m-p/588628#M168252</link>
      <description>Thank you, I was asked to use only data step&lt;BR /&gt;</description>
      <pubDate>Fri, 13 Sep 2019 18:45:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Applying-format-in-SQL-table/m-p/588628#M168252</guid>
      <dc:creator>Babloo</dc:creator>
      <dc:date>2019-09-13T18:45:44Z</dc:date>
    </item>
    <item>
      <title>Re: Applying format in SQL table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Applying-format-in-SQL-table/m-p/588817#M168342</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/8409"&gt;@Babloo&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Thank you, I was asked to use only data step&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;That's a strange requirement. But o.k! Given your initial post something like below could work. This assumes that only reading the first row from your source table like in the code you've posted is what you need.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options sastrace='d' sastraceloc=saslog nostsuffix;
data SQLLIB.SQL_table;
	set metadata_final(rename=(STAT_CD=_STAT_CD));
	run_id=sum(n,1);
  STAT_CD=put(_STAT_CD,$STAT_CD.);
  drop _STAT_CD;
	output;
	stop;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If it was me then I'd split the process into first preparing the data in SAS and then use Proc Append for the Insert into SQL Server.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options sastrace='d' sastraceloc=saslog nostsuffix;
data prep(drop=_stat_cd);
	set metadata_final(obs=1 rename=(STAT_CD=_STAT_CD));
	run_id=sum(n,1);
  STAT_CD=put(_STAT_CD,$STAT_CD.);
run;

proc append base=SQLLIB.SQL_table data=prep;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Alternatively:&lt;/P&gt;
&lt;P&gt;- Load the data "as is" into SQL (=unformatted values)&lt;/P&gt;
&lt;P&gt;- Load the SAS format as key/value pairs into another SQL table (you can easily get such a table by using Proc Format with CNTLOUT)&lt;/P&gt;
&lt;P&gt;- Create a view in Oracle which left-joins with the table with key/value pairs to create the column with formatted values&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The advantage of such an approach: If you ever have to amend the format on the SAS side then you have only to re-load the key/value table in SQL but you don't need to come-up with code which changes the values in your main table (something you would need to do if loading the formatted values only).&lt;/P&gt;</description>
      <pubDate>Mon, 16 Sep 2019 08:42:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Applying-format-in-SQL-table/m-p/588817#M168342</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2019-09-16T08:42:58Z</dc:date>
    </item>
  </channel>
</rss>

