<?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: How do I get result using substring in proc sql? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-get-result-using-substring-in-proc-sql/m-p/303278#M64393</link>
    <description>&lt;P&gt;You cannot start 5 characters before the start of the string. 1 is the lowest value allowed to start from.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You would likely get better results with: put(customerid,z5.) as newid&lt;/P&gt;
&lt;P&gt;if your customerid is indeed numeric.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would wager that somewhere in the begining that a data set was created using Proc Import that treated your customerid as numeric. Or possible Excel was involved and did the same thing, stripping leading zeroes.&lt;/P&gt;</description>
    <pubDate>Fri, 07 Oct 2016 19:51:44 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2016-10-07T19:51:44Z</dc:date>
    <item>
      <title>How do I get result using substring in proc sql?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-get-result-using-substring-in-proc-sql/m-p/303271#M64391</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;The Orders table in library sales has a variable CustomerId char(3).&lt;BR /&gt;&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;On running below both the SQL, creates table with no result in variable NewID.&amp;nbsp; &lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000000"&gt;Environment: SAS base 9.2 &lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;Log shows note as : &lt;FONT color="#0000FF"&gt;NOTE: Invalid argument 2 to function SUBSTR. Missing values may be generate&lt;/FONT&gt;d&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;FONT color="#000080"&gt;How to get result in the variable NewId as 00123&amp;nbsp; from 123.&amp;nbsp;&amp;nbsp; &lt;/FONT&gt;&lt;/P&gt;&lt;PRE&gt;Option 1
proc sql;
   create table NewList as
   select unique substr(cat('00000',CustomerId),-5,5) as NewID,  
          customerid
   from   Sales.orders;
quit;

Option 2
proc sql;
create table NewList as
select unique input(substr(cat('00000',CustomerId),-5,5),$char5.) as NewID,
       customerid
from   Sales.orders;
quit;&lt;/PRE&gt;&lt;P&gt;Thanks,&lt;BR /&gt;UA&lt;/P&gt;</description>
      <pubDate>Fri, 07 Oct 2016 19:40:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-get-result-using-substring-in-proc-sql/m-p/303271#M64391</guid>
      <dc:creator>SAS_UA</dc:creator>
      <dc:date>2016-10-07T19:40:11Z</dc:date>
    </item>
    <item>
      <title>Re: How do I get result using substring in proc sql?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-get-result-using-substring-in-proc-sql/m-p/303278#M64393</link>
      <description>&lt;P&gt;You cannot start 5 characters before the start of the string. 1 is the lowest value allowed to start from.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You would likely get better results with: put(customerid,z5.) as newid&lt;/P&gt;
&lt;P&gt;if your customerid is indeed numeric.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would wager that somewhere in the begining that a data set was created using Proc Import that treated your customerid as numeric. Or possible Excel was involved and did the same thing, stripping leading zeroes.&lt;/P&gt;</description>
      <pubDate>Fri, 07 Oct 2016 19:51:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-get-result-using-substring-in-proc-sql/m-p/303278#M64393</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-10-07T19:51:44Z</dc:date>
    </item>
    <item>
      <title>Re: How do I get result using substring in proc sql?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-get-result-using-substring-in-proc-sql/m-p/303317#M64410</link>
      <description>&lt;P&gt;Hi Ballardw,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Need the numbers from right side with left side zero padding, so that can use the table to connect with other data tables via proc sql.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1) customer id is character.&amp;nbsp;&lt;/P&gt;&lt;P&gt;2) substr gives error on using minus sign for extraction e.g. substr(cat('00000',customerid),-1,6) does not work.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there a way to extract numbers from right, after concatenating ('00000',Customerid) through SAS and&lt;/P&gt;&lt;P&gt;&amp;nbsp;store value in the table as 000123 or 000012 based on customerid value ?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thx&lt;/P&gt;&lt;P&gt;UA&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 07 Oct 2016 22:14:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-get-result-using-substring-in-proc-sql/m-p/303317#M64410</guid>
      <dc:creator>SAS_UA</dc:creator>
      <dc:date>2016-10-07T22:14:19Z</dc:date>
    </item>
    <item>
      <title>Re: How do I get result using substring in proc sql?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-get-result-using-substring-in-proc-sql/m-p/303321#M64412</link>
      <description>&lt;P&gt;Did you attempt the code line I provided? PUTting data with Z format pads the displayed value with zeroes on the left.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PUT(123, z5.) as NewId&amp;nbsp;will yield a character variable of '00123'&lt;/P&gt;</description>
      <pubDate>Fri, 07 Oct 2016 22:33:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-get-result-using-substring-in-proc-sql/m-p/303321#M64412</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-10-07T22:33:12Z</dc:date>
    </item>
    <item>
      <title>Re: How do I get result using substring in proc sql?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-get-result-using-substring-in-proc-sql/m-p/303360#M64427</link>
      <description>&lt;P&gt;- use the z. format, as &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw﻿&lt;/a&gt; already recommended.&lt;/P&gt;
&lt;P&gt;- to extract the last x characters from a string, use the length() function to find the end:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
x1 = '00000123';
x = 5;
x2 = substr(x1,length(x1)-x+1);
put x2=;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Negative indexes (to start from the end) only work in functions like scan(), but not in substr().&lt;/P&gt;</description>
      <pubDate>Sat, 08 Oct 2016 09:02:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-get-result-using-substring-in-proc-sql/m-p/303360#M64427</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-10-08T09:02:01Z</dc:date>
    </item>
    <item>
      <title>Re: How do I get result using substring in proc sql?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-get-result-using-substring-in-proc-sql/m-p/303370#M64429</link>
      <description>&lt;P&gt;Hi Ballardw,&lt;/P&gt;&lt;P&gt;Thanks, thought z is only used for numeric.&amp;nbsp; tested it worked.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;KurtBremser:&lt;/P&gt;&lt;P&gt;Thanks to you also, for the the datastep and clarifying minus sign only works with scan().&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;UA&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 08 Oct 2016 11:26:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-get-result-using-substring-in-proc-sql/m-p/303370#M64429</guid>
      <dc:creator>SAS_UA</dc:creator>
      <dc:date>2016-10-08T11:26:34Z</dc:date>
    </item>
    <item>
      <title>Re: How do I get result using substring in proc sql?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-get-result-using-substring-in-proc-sql/m-p/303378#M64430</link>
      <description>&lt;P&gt;You&amp;nbsp;&lt;U&gt;can&lt;/U&gt; use a numeric format on a character variable, but that forces SAS to make an automatic conversion.&lt;/P&gt;
&lt;P&gt;I'd rather do the conversion explicitly:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;x = put(input(x,best.),z5.);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This prevents the NOTE about the conversion in the log and shows the next one who has to maintain the code that you knew what you were doing.&lt;/P&gt;
&lt;P&gt;Hint: the next one might be you.&lt;/P&gt;</description>
      <pubDate>Sat, 08 Oct 2016 17:42:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-get-result-using-substring-in-proc-sql/m-p/303378#M64430</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-10-08T17:42:06Z</dc:date>
    </item>
  </channel>
</rss>

