<?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 Modify Column data types and format on a table based on another table in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Modify-Column-data-types-and-format-on-a-table-based-on-another/m-p/539985#M148891</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am quite new to SAS, pardon me if my question is naive.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to change my target table definition (TARGET) based on my Source table (SOURCE) column data type&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As part of my programming first I create a table TARGET with the default data type and column length as char(30).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;"AS-IS" Table definition&amp;nbsp;for TARGET:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;| Name&amp;nbsp; | Type | Length&lt;/P&gt;&lt;P&gt;| ID&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;| Char | 30&lt;/P&gt;&lt;P&gt;| Name&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;| Char | 30&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;"AS-IS" Table definition&amp;nbsp;for SOURCE:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;| Name&amp;nbsp; | Type | Length&lt;/P&gt;&lt;P&gt;| ID&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; | Char&amp;nbsp; &amp;nbsp; &amp;nbsp; | 30&lt;/P&gt;&lt;P&gt;| Name&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; | Numeric | 8&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;TARGET table is a flat/empty table, where I need to update the values from the Source table. So first I need to convert the data type of the TARGET table based on the SOURCE table.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;"To-Be"&amp;nbsp;Table definition&amp;nbsp;for TARGET:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;| Name&amp;nbsp; | Type | Length&lt;/P&gt;&lt;P&gt;| ID&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; | Char&amp;nbsp; &amp;nbsp; &amp;nbsp; | 30&lt;/P&gt;&lt;P&gt;| Name&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; | Numeric | 8&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Following is the code I have written&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;proc sql;

Create table Test( name char(14)) ;

quit;



proc sql;

select cat(type,'(',length,')'),format into :Dtype,:format from dictionary.columns where libname = 'WORK' and memname = 'SOURCE' and upcase(name)='VALUE' ;

quit;



proc sql;

alter table WORK.Test modify name &amp;amp;Dtype. format=&amp;amp;format.;

quit;&lt;/PRE&gt;&lt;P&gt;I have an error message:&amp;nbsp;&lt;/P&gt;&lt;P&gt;ERROR: You cannot alter 'name' to be a numeric column.&lt;BR /&gt;ERROR: Character column name requires a character format specification.&lt;/P&gt;</description>
    <pubDate>Mon, 04 Mar 2019 09:55:41 GMT</pubDate>
    <dc:creator>Sudhan</dc:creator>
    <dc:date>2019-03-04T09:55:41Z</dc:date>
    <item>
      <title>Modify Column data types and format on a table based on another table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Modify-Column-data-types-and-format-on-a-table-based-on-another/m-p/539985#M148891</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am quite new to SAS, pardon me if my question is naive.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to change my target table definition (TARGET) based on my Source table (SOURCE) column data type&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As part of my programming first I create a table TARGET with the default data type and column length as char(30).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;"AS-IS" Table definition&amp;nbsp;for TARGET:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;| Name&amp;nbsp; | Type | Length&lt;/P&gt;&lt;P&gt;| ID&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;| Char | 30&lt;/P&gt;&lt;P&gt;| Name&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;| Char | 30&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;"AS-IS" Table definition&amp;nbsp;for SOURCE:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;| Name&amp;nbsp; | Type | Length&lt;/P&gt;&lt;P&gt;| ID&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; | Char&amp;nbsp; &amp;nbsp; &amp;nbsp; | 30&lt;/P&gt;&lt;P&gt;| Name&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; | Numeric | 8&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;TARGET table is a flat/empty table, where I need to update the values from the Source table. So first I need to convert the data type of the TARGET table based on the SOURCE table.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;"To-Be"&amp;nbsp;Table definition&amp;nbsp;for TARGET:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;| Name&amp;nbsp; | Type | Length&lt;/P&gt;&lt;P&gt;| ID&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; | Char&amp;nbsp; &amp;nbsp; &amp;nbsp; | 30&lt;/P&gt;&lt;P&gt;| Name&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; | Numeric | 8&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Following is the code I have written&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;proc sql;

Create table Test( name char(14)) ;

quit;



proc sql;

select cat(type,'(',length,')'),format into :Dtype,:format from dictionary.columns where libname = 'WORK' and memname = 'SOURCE' and upcase(name)='VALUE' ;

quit;



proc sql;

alter table WORK.Test modify name &amp;amp;Dtype. format=&amp;amp;format.;

quit;&lt;/PRE&gt;&lt;P&gt;I have an error message:&amp;nbsp;&lt;/P&gt;&lt;P&gt;ERROR: You cannot alter 'name' to be a numeric column.&lt;BR /&gt;ERROR: Character column name requires a character format specification.&lt;/P&gt;</description>
      <pubDate>Mon, 04 Mar 2019 09:55:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Modify-Column-data-types-and-format-on-a-table-based-on-another/m-p/539985#M148891</guid>
      <dc:creator>Sudhan</dc:creator>
      <dc:date>2019-03-04T09:55:41Z</dc:date>
    </item>
    <item>
      <title>Re: Modify Column data types and format on a table based on another table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Modify-Column-data-types-and-format-on-a-table-based-on-another/m-p/539988#M148893</link>
      <description>&lt;P&gt;The error quite clearly tells you the problem, you cannot change the type of a column.&amp;nbsp; How can name be both character and numeric?&amp;nbsp; If it contains non-numeric information, then it is not a numeric field.&amp;nbsp; The general rule would be, create the structure, then add the data into the given structure, so an example:&lt;/P&gt;
&lt;PRE&gt;proc sql;
  create table target (id char(30),name char(30));
quit;

data target (drop=_name);
  set target
        /* Here I move the data to a new column so as not to confuse the 
           compiler with a numeric and character field of the same name */
        source (rename=(name=_name));
  /* Here I take the numeric data and put it into text in the character field */
  name=strip(put(_name,best.));
run;
&lt;/PRE&gt;
&lt;P&gt;What this does is create a temporary variable _name which is dropped at the end, which contains the numeric data, I put() this into the character variable.&amp;nbsp; No alteration of the base table is done.&amp;nbsp; You can of course do it the other way and convert character to numeric - however be aware that will fail if there is non-numeric data:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc sql;
  create table target (id char(30),name num);
quit;

data target (drop=_name);
  set target
        source (rename=(name=_name));
  name=input(_name,best.);
run;&lt;/PRE&gt;</description>
      <pubDate>Mon, 04 Mar 2019 10:04:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Modify-Column-data-types-and-format-on-a-table-based-on-another/m-p/539988#M148893</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2019-03-04T10:04:55Z</dc:date>
    </item>
    <item>
      <title>Re: Modify Column data types and format on a table based on another table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Modify-Column-data-types-and-format-on-a-table-based-on-another/m-p/539989#M148894</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/259982"&gt;@Sudhan&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Changing a column type from Character to Numeric is not possible because type Numeric wouldn't be able to store data as alphanumeric strings. The SQL doesn't "care" that your table is empty.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Why do you first create the target table structure if you actually don't want to use it? That's not necessary when using SAS as SAS will in most cases just (re-) create the target table and it will use the column definitions from source to do so.&lt;/P&gt;</description>
      <pubDate>Tue, 05 Mar 2019 05:15:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Modify-Column-data-types-and-format-on-a-table-based-on-another/m-p/539989#M148894</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2019-03-05T05:15:19Z</dc:date>
    </item>
    <item>
      <title>Re: Modify Column data types and format on a table based on another table</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Modify-Column-data-types-and-format-on-a-table-based-on-another/m-p/540000#M148902</link>
      <description>Since you will need a data step anyway, why use SQL at all?  Why not:&lt;BR /&gt;&lt;BR /&gt;data want;&lt;BR /&gt;set have;&lt;BR /&gt;newvar = put ( name, 30. );&lt;BR /&gt;drop name;&lt;BR /&gt;rename newvar = name;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;The new version is right-hand justified, although you could easily add to the program to change that.</description>
      <pubDate>Mon, 04 Mar 2019 11:51:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Modify-Column-data-types-and-format-on-a-table-based-on-another/m-p/540000#M148902</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-03-04T11:51:27Z</dc:date>
    </item>
  </channel>
</rss>

