<?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 isolate select digits from a numeric key based on varying conditions? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-isolate-select-digits-from-a-numeric-key-based-on/m-p/551389#M153184</link>
    <description>&lt;P&gt;Is COUNTRY_ID defined as numeric type or as char type?&lt;/P&gt;
&lt;P&gt;If it is numeric define format as 3. to avoid leading zero.&lt;/P&gt;
&lt;P&gt;If it is char type then define:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token keyword"&gt;if&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;length&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;key&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;8&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;THEN&lt;/SPAN&gt; COUNTRY_ID&lt;SPAN class="token operator"&gt;=put(input(&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;substr&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;key&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;2&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;3&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;),3.),3.)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 16 Apr 2019 14:40:32 GMT</pubDate>
    <dc:creator>Shmuel</dc:creator>
    <dc:date>2019-04-16T14:40:32Z</dc:date>
    <item>
      <title>How do I isolate select digits from a numeric key based on varying conditions?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-isolate-select-digits-from-a-numeric-key-based-on/m-p/551383#M153181</link>
      <description>&lt;P&gt;I am trying to extract a Country ID from a numeric product key which has 2 types:&lt;BR /&gt;1. A 7-8 digit key with first 2-3 digits signifying country ID.&lt;BR /&gt;i.e. For US with Country code 36, product key= 36XXXXX&lt;/P&gt;&lt;P&gt;For INDIA with Country code 121, product key= 121XXXXX&lt;/P&gt;&lt;P&gt;2. A 13 digit key with 2nd, 3rd , 4th digit signifying country ID&lt;BR /&gt;i.e. For US , Product key=X036XXXXXXXXX&lt;/P&gt;&lt;P&gt;For IN, Product Key= X121XXXXXXXXX&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Using the following code to get this:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
if length(key)&amp;gt;8 THEN COUNTRY_ID=substr(key), 2,3);
else COUNTRY_ID=substr(key,1,length(key)-5);
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;It works for the 1st type with 7-8 digit product key but not for the longer 13 digit keys. It shows a decimal in the country id&lt;BR /&gt;i.e. For US, X036XXXX whereas it should be just 36.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Do I need to change data type??&lt;BR /&gt;&lt;BR /&gt;Using SAS EG 7.1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 16 Apr 2019 14:21:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-isolate-select-digits-from-a-numeric-key-based-on/m-p/551383#M153181</guid>
      <dc:creator>Gaurangl</dc:creator>
      <dc:date>2019-04-16T14:21:37Z</dc:date>
    </item>
    <item>
      <title>Re: How do I isolate select digits from a numeric key based on varying conditions?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-isolate-select-digits-from-a-numeric-key-based-on/m-p/551386#M153182</link>
      <description>&lt;P&gt;First thing you need to pay attention when your LOG shows lines like:&lt;/P&gt;
&lt;PRE&gt;NOTE: Numeric values have been converted to character values at the places given by:
      (Line):(Column).
      14:13
&lt;/PRE&gt;
&lt;P&gt;Since you did not specify a format to convert a numeric to character when the Length function&amp;nbsp; is called for the numeric value then SAS does a type conversion and defaults to using a BEST12. format. So the length returned to begin with is not what think was returned. Please see this example:&lt;/P&gt;
&lt;PRE&gt;data example;
   input x;
   l=length(x);
datalines;
1
12
123
1234
12345
123456
1234567
12345678
123456789
1234567890
12345678901
123456789012
1234567890123
;
run;&lt;/PRE&gt;
&lt;P&gt;The "Length" for all of those values is 12 because of the way the automatic conversion is done.&lt;/P&gt;
&lt;P&gt;Better would have been to insure the values were read that they&amp;nbsp;were read as character values if you are not going to do arithmetic with them.&lt;/P&gt;
&lt;P&gt;See this for a way to get the proper Length comparison:&lt;/P&gt;
&lt;PRE&gt;data example;
   input x;
   l=length(put(x,15. -L));
datalines;
1
12
123
1234
12345
123456
1234567
12345678
123456789
1234567890
12345678901
123456789012
1234567890123
;
run;&lt;/PRE&gt;
&lt;P&gt;The -L says to left justify the result of PUT.&lt;/P&gt;
&lt;P&gt;You also need to use the same sort of PUT (Key,15. -L) in the SUBSTR function argument so you aren't getting leading spaces and such.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 16 Apr 2019 14:33:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-isolate-select-digits-from-a-numeric-key-based-on/m-p/551386#M153182</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-04-16T14:33:31Z</dc:date>
    </item>
    <item>
      <title>Re: How do I isolate select digits from a numeric key based on varying conditions?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-isolate-select-digits-from-a-numeric-key-based-on/m-p/551389#M153184</link>
      <description>&lt;P&gt;Is COUNTRY_ID defined as numeric type or as char type?&lt;/P&gt;
&lt;P&gt;If it is numeric define format as 3. to avoid leading zero.&lt;/P&gt;
&lt;P&gt;If it is char type then define:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token keyword"&gt;if&lt;/SPAN&gt; &lt;SPAN class="token function"&gt;length&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;key&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;&amp;gt;&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;8&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;THEN&lt;/SPAN&gt; COUNTRY_ID&lt;SPAN class="token operator"&gt;=put(input(&lt;/SPAN&gt;&lt;SPAN class="token function"&gt;substr&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;key&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;2&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;,&lt;/SPAN&gt;&lt;SPAN class="token number"&gt;3&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;),3.),3.)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 16 Apr 2019 14:40:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-isolate-select-digits-from-a-numeric-key-based-on/m-p/551389#M153184</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2019-04-16T14:40:32Z</dc:date>
    </item>
  </channel>
</rss>

