<?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: Proc SQL: insert into... select... trim char? in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Proc-SQL-insert-into-select-trim-char/m-p/572862#M12382</link>
    <description>&lt;P&gt;Note you can just let SAS truncate the value without needing to use the SUBSTR() function. Even when the value is longer than the targeted length.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data table1; length field $20; field='abc4567890abc'; run;
data table2; length field $10; stop; run;

proc sql;
  insert into table2 (field)
    select * from (select field length=10 from table1)
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 11 Jul 2019 17:17:55 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2019-07-11T17:17:55Z</dc:date>
    <item>
      <title>Proc SQL: insert into... select... trim char?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Proc-SQL-insert-into-select-trim-char/m-p/572633#M12356</link>
      <description>&lt;P&gt;I am trying to move data from one table to another using proc SQL. A minimal example is given below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql; 
	create table table1
		(field varchar(20) not null);
	create table table2
		(field varchar(10) not null);
	insert into table1
	values ('abc');
quit;

proc sql;
	insert into table2 (field)
	select strip(field)
	from table1;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Obviously this produces a warning message that the string of length 20 from table1 will be truncated when inserted into table2, where the respective column contains strings of length 10.&lt;BR /&gt;&lt;BR /&gt;In order to remove this warning I tried to remove trailing blanks or consider only the first half of the string - but none of this seems to work:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
	insert into table2 (field)
	select strip(field)
	from table1;
quit;


proc sql;
	insert into table2 (field)
	select substr(field, 1, 10)
	from table1;
quit;


proc sql;
	insert into table2 (field)
	select trim(field)
	from table1;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Does anybody know&amp;nbsp;why trimming does not work in this Situation and whether there is a Workaround?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Best regards,&lt;BR /&gt;Jonathan&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 11 Jul 2019 08:26:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Proc-SQL-insert-into-select-trim-char/m-p/572633#M12356</guid>
      <dc:creator>JGA1</dc:creator>
      <dc:date>2019-07-11T08:26:55Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL: insert into... select... trim char?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Proc-SQL-insert-into-select-trim-char/m-p/572637#M12359</link>
      <description>&lt;P&gt;Create a sub-select where you explicitly set the length:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  insert into table2 (field)
  select * from
  (select substr(field, 1, 10) length=10
  from table1);
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 11 Jul 2019 08:37:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Proc-SQL-insert-into-select-trim-char/m-p/572637#M12359</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-07-11T08:37:14Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL: insert into... select... trim char?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Proc-SQL-insert-into-select-trim-char/m-p/572638#M12360</link>
      <description>Thank you! That works perfectly!</description>
      <pubDate>Thu, 11 Jul 2019 09:07:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Proc-SQL-insert-into-select-trim-char/m-p/572638#M12360</guid>
      <dc:creator>JGA1</dc:creator>
      <dc:date>2019-07-11T09:07:58Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL: insert into... select... trim char?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Proc-SQL-insert-into-select-trim-char/m-p/572852#M12381</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/281204"&gt;@JGA1&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I am trying to move data from one table to another using proc SQL. A minimal example is given below:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql; 
	create table table1
		(field varchar(20) not null);
	create table table2
		(field varchar(10) not null);
	insert into table1
	values ('abc');
quit;

proc sql;
	insert into table2 (field)
	select strip(field)
	from table1;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Obviously this produces a warning message that the string of length 20 from table1 will be truncated when inserted into table2, where the respective column contains strings of length 10.&lt;BR /&gt;&lt;BR /&gt;In order to remove this warning I tried to remove trailing blanks or consider only the first half of the string - but none of this seems to work:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
	insert into table2 (field)
	select strip(field)
	from table1;
quit;


proc sql;
	insert into table2 (field)
	select substr(field, 1, 10)
	from table1;
quit;


proc sql;
	insert into table2 (field)
	select trim(field)
	from table1;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Does anybody know&amp;nbsp;why trimming does not work in this Situation and whether there is a Workaround?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best regards,&lt;BR /&gt;Jonathan&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The "why" is that the error message relates to the defined lengths of the variables, not the content. Trim does not change the length of the variable storage space it would just change (possibly)&amp;nbsp;the charters used for some operation. You can get the truncation waringin without any actual value at all:&lt;/P&gt;
&lt;PRE&gt;9    data example;
10      length x $ 15;
11   run;

NOTE: Variable x is uninitialized.
NOTE: The data set USER.EXAMPLE has 1 observations and 1 variables.
NOTE: DATA statement used (Total process time):
      real time           0.02 seconds
      cpu time            0.00 seconds


12   data example2;
13     length x $10;
14     set example;
15   run;

WARNING: Multiple lengths were specified for the variable x by input data set(s). This can cause
         truncation of data.
NOTE: There were 1 observations read from the data set USER.EXAMPLE.
NOTE: The data set USER.EXAMPLE2 has 1 observations and 1 variables.
NOTE: DATA statement used (Total process time):
      real time           0.02 seconds
      cpu time
&lt;/PRE&gt;</description>
      <pubDate>Thu, 11 Jul 2019 17:09:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Proc-SQL-insert-into-select-trim-char/m-p/572852#M12381</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-07-11T17:09:17Z</dc:date>
    </item>
    <item>
      <title>Re: Proc SQL: insert into... select... trim char?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Proc-SQL-insert-into-select-trim-char/m-p/572862#M12382</link>
      <description>&lt;P&gt;Note you can just let SAS truncate the value without needing to use the SUBSTR() function. Even when the value is longer than the targeted length.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data table1; length field $20; field='abc4567890abc'; run;
data table2; length field $10; stop; run;

proc sql;
  insert into table2 (field)
    select * from (select field length=10 from table1)
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 11 Jul 2019 17:17:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Proc-SQL-insert-into-select-trim-char/m-p/572862#M12382</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-07-11T17:17:55Z</dc:date>
    </item>
  </channel>
</rss>

