<?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: extracting first few digits from a numeric variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/extracting-first-few-digits-from-a-numeric-variable/m-p/325726#M265459</link>
    <description>&lt;P&gt;I don't think this is the correct answer. That will not extract first 4 values. rather then it will give you blank value.&lt;/P&gt;
&lt;P&gt;the one way which I can think off. we can use it as below.&lt;/P&gt;
&lt;P&gt;data one;&lt;BR /&gt; val=1234567;&lt;BR /&gt; val1=substr(left(val),1,4);&lt;BR /&gt; run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 18 Jan 2017 16:46:06 GMT</pubDate>
    <dc:creator>PravinMishra</dc:creator>
    <dc:date>2017-01-18T16:46:06Z</dc:date>
    <item>
      <title>extracting first few digits from a numeric variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extracting-first-few-digits-from-a-numeric-variable/m-p/184315#M265437</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Newbbie needs help: How to retrieve the first few digits from a numeric variable in numeric format?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;For example, I have a numeric variable containing a number 123456789. I want to retrieve the first 4 digits 1234 into a new numeric variable. Of course I can put the numeric variable into character, then use substr, then later I can convert character back into numeric.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I wonder if there is a more easier and straight forward method to do it. Thanks.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 03 Sep 2014 11:51:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extracting-first-few-digits-from-a-numeric-variable/m-p/184315#M265437</guid>
      <dc:creator>cn1414</dc:creator>
      <dc:date>2014-09-03T11:51:17Z</dc:date>
    </item>
    <item>
      <title>Re: extracting first few digits from a numeric variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extracting-first-few-digits-from-a-numeric-variable/m-p/184316#M265438</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;If it is that simple then basic maths, divide by the number of digits you want to remove then int the result:&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;&amp;nbsp; a=123456789;&lt;/P&gt;&lt;P&gt;&amp;nbsp; b=int(a/100000);&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 03 Sep 2014 12:15:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extracting-first-few-digits-from-a-numeric-variable/m-p/184316#M265438</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2014-09-03T12:15:36Z</dc:date>
    </item>
    <item>
      <title>Re: extracting first few digits from a numeric variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extracting-first-few-digits-from-a-numeric-variable/m-p/184317#M265439</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Assuming they are all positive value.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;data _null_;
 x=123456789;
 y=int( divide(x,10**(int(log10(x))-3)) );
 put x= y= ;
run;

&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Xia Keshan&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 03 Sep 2014 12:17:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extracting-first-few-digits-from-a-numeric-variable/m-p/184317#M265439</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2014-09-03T12:17:03Z</dc:date>
    </item>
    <item>
      <title>Re: extracting first few digits from a numeric variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extracting-first-few-digits-from-a-numeric-variable/m-p/184318#M265440</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks for your help. But my point actually is, for character variables, it is very simple to retrieve the first 4 char using substr. What I am wondering is, is there any simple method to use to in a similar way for char variables, without having to convert numeric into char first, then using substr, then convert back to numerical again.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 03 Sep 2014 13:05:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extracting-first-few-digits-from-a-numeric-variable/m-p/184318#M265440</guid>
      <dc:creator>cn1414</dc:creator>
      <dc:date>2014-09-03T13:05:40Z</dc:date>
    </item>
    <item>
      <title>Re: extracting first few digits from a numeric variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extracting-first-few-digits-from-a-numeric-variable/m-p/184319#M265441</link>
      <description>&lt;P&gt;&lt;EM&gt;Editors note: Thanks to RW9 and all the others that provided different ways to retrieve N number of characters from a value.&amp;nbsp; RW9 is correct in that we must convert to character to achieve this goal.&amp;nbsp; For example:&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;&amp;nbsp;data one;&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;&amp;nbsp; val=1234567;&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;&amp;nbsp; val=substr(val,1,4);&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;&amp;nbsp;run;&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;This will keep the original variable as numeric but an implied conversion from numeric to character must take place.&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Nope, numbers are not strings, hence you cannot do string functions on them without converting to character (even if the conversion is implicit).&amp;nbsp; You can utilize maths to give the same effect per above, however the underlying storage of a string (which is an array of characters) is very different to a number.&lt;/P&gt;</description>
      <pubDate>Wed, 21 Sep 2016 20:13:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extracting-first-few-digits-from-a-numeric-variable/m-p/184319#M265441</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-09-21T20:13:04Z</dc:date>
    </item>
    <item>
      <title>Re: extracting first few digits from a numeric variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extracting-first-few-digits-from-a-numeric-variable/m-p/184320#M265442</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks a lot. Makes sense...&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 03 Sep 2014 14:17:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extracting-first-few-digits-from-a-numeric-variable/m-p/184320#M265442</guid>
      <dc:creator>cn1414</dc:creator>
      <dc:date>2014-09-03T14:17:20Z</dc:date>
    </item>
    <item>
      <title>Re: extracting first few digits from a numeric variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extracting-first-few-digits-from-a-numeric-variable/m-p/184321#M265443</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Newvar = input(substr(put(var,best12.),1,4),best12.);&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 03 Sep 2014 14:40:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extracting-first-few-digits-from-a-numeric-variable/m-p/184321#M265443</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2014-09-03T14:40:06Z</dc:date>
    </item>
    <item>
      <title>Re: extracting first few digits from a numeric variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extracting-first-few-digits-from-a-numeric-variable/m-p/184322#M265444</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;DATA HAVE;&lt;BR /&gt;INPUT NUM;&lt;BR /&gt;DATALINES;&lt;BR /&gt;1234567&lt;BR /&gt;2345&lt;BR /&gt;456676&lt;BR /&gt;234335&lt;BR /&gt;;&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;DATA WANT;&lt;BR /&gt;SET HAVE;&lt;BR /&gt;FIN=SUBSTRN(NUM,2,4);&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;IMG alt="Capture.JPG" class="jive-image" src="https://communities.sas.com/legacyfs/online/7272_Capture.JPG" /&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 03 Sep 2014 14:47:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extracting-first-few-digits-from-a-numeric-variable/m-p/184322#M265444</guid>
      <dc:creator>Hima</dc:creator>
      <dc:date>2014-09-03T14:47:35Z</dc:date>
    </item>
    <item>
      <title>Re: extracting first few digits from a numeric variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extracting-first-few-digits-from-a-numeric-variable/m-p/184323#M265445</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Still an implicit conversion from numeric to character done in that function.&lt;BR /&gt;&lt;A href="http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a002255112.htm" title="http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a002255112.htm"&gt;SAS(R) 9.2 Language Reference: Dictionary, Fourth Edition&lt;/A&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 03 Sep 2014 14:56:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extracting-first-few-digits-from-a-numeric-variable/m-p/184323#M265445</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2014-09-03T14:56:28Z</dc:date>
    </item>
    <item>
      <title>Re: extracting first few digits from a numeric variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extracting-first-few-digits-from-a-numeric-variable/m-p/184324#M265446</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Got it Thank you.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 03 Sep 2014 15:00:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extracting-first-few-digits-from-a-numeric-variable/m-p/184324#M265446</guid>
      <dc:creator>Hima</dc:creator>
      <dc:date>2014-09-03T15:00:11Z</dc:date>
    </item>
    <item>
      <title>Re: extracting first few digits from a numeric variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extracting-first-few-digits-from-a-numeric-variable/m-p/184325#M265447</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Just an edit to what I posted,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;DATA HAVE;&lt;BR /&gt;INPUT NUM;&lt;BR /&gt;DATALINES;&lt;BR /&gt;1234567&lt;BR /&gt;2345&lt;BR /&gt;456676&lt;BR /&gt;234335&lt;BR /&gt;;&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;DATA WANT;&lt;BR /&gt;SET HAVE;&lt;BR /&gt;&lt;SPAN style="text-decoration: underline;"&gt;&lt;STRONG&gt;FIN=SUBSTRN(NUM,2,4)*1;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;PROC CONTENTS DATA=WANT;&lt;/P&gt;&lt;P&gt;RUN;&lt;BR /&gt;&lt;IMG __jive_id="7273" alt="Capture.JPG" class="jive-image" src="https://communities.sas.com/legacyfs/online/7273_Capture.JPG" /&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 03 Sep 2014 15:03:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extracting-first-few-digits-from-a-numeric-variable/m-p/184325#M265447</guid>
      <dc:creator>Hima</dc:creator>
      <dc:date>2014-09-03T15:03:30Z</dc:date>
    </item>
    <item>
      <title>Re: extracting first few digits from a numeric variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extracting-first-few-digits-from-a-numeric-variable/m-p/184326#M265448</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Wow!!&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 03 Sep 2014 15:17:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extracting-first-few-digits-from-a-numeric-variable/m-p/184326#M265448</guid>
      <dc:creator>cn1414</dc:creator>
      <dc:date>2014-09-03T15:17:23Z</dc:date>
    </item>
    <item>
      <title>Re: extracting first few digits from a numeric variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extracting-first-few-digits-from-a-numeric-variable/m-p/184327#M265449</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Actually two implicit conversions there.&amp;nbsp; The substrn internally converts numbers to character:&lt;/P&gt;&lt;P&gt;---&lt;/P&gt;&lt;P&gt;&lt;SPAN class="strongEmph"&gt;string&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;A name="a002896268"&gt;&lt;/A&gt;specifies a character or numeric constant, variable, or expression.&lt;/P&gt;&lt;P&gt;If &lt;SPAN class="strongEmph"&gt;string&lt;/SPAN&gt; is numeric, then it is converted to a character value that uses the BEST32. format. Leading and trailing blanks are removed, and no message is sent to the SAS log.&lt;/P&gt;&lt;P&gt;---&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Then by invoking the * mathematical symbol it attempts to convert characters to numbers and multiply the result.&amp;nbsp; It may work, but you will get issues if the character start to contain odd things (say -12345.67 for example).&amp;nbsp; And also note the note put into the log:&lt;/P&gt;&lt;P&gt;---&lt;/P&gt;&lt;P&gt;NOTE: Character values have been converted to numeric values at the places given by: (Line):(Column).&lt;/P&gt;&lt;P&gt;---&lt;/P&gt;&lt;P&gt;Would be unacceptable to my log checking.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Depends really on what you want the "first 4 digits" for, I would recommend explicit casting of the data - it maybe a step or two more, but is far more readable.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 03 Sep 2014 15:25:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extracting-first-few-digits-from-a-numeric-variable/m-p/184327#M265449</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2014-09-03T15:25:24Z</dc:date>
    </item>
    <item>
      <title>Re: extracting first few digits from a numeric variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extracting-first-few-digits-from-a-numeric-variable/m-p/184328#M265450</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Here is a solution that avoids number to character conversion and back again, and also deals with fractional and negative values&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int(abs(num)/10**(log10(abs(num))-3))&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;It works by dividing the number by the requisite power of 10 (including negative power) and truncating the decimal portion.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Richard&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 05 Sep 2014 22:58:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extracting-first-few-digits-from-a-numeric-variable/m-p/184328#M265450</guid>
      <dc:creator>RichardinOz</dc:creator>
      <dc:date>2014-09-05T22:58:15Z</dc:date>
    </item>
    <item>
      <title>Re: extracting first few digits from a numeric variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extracting-first-few-digits-from-a-numeric-variable/m-p/184329#M265451</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Would not go into any details as there are 10 persons understanding the digital world.&lt;BR /&gt;&lt;A class="active_link" href="https://support.sas.com/techsup/technote/ts654.pdf" title="https://support.sas.com/techsup/technote/ts654.pdf"&gt;https://support.sas.com/techsup/technote/ts654.pdf&lt;/A&gt;&lt;/P&gt;&lt;P&gt;Numerics floating and the digital representation? Take also notice of the precision. &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 06 Sep 2014 07:53:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extracting-first-few-digits-from-a-numeric-variable/m-p/184329#M265451</guid>
      <dc:creator>jakarman</dc:creator>
      <dc:date>2014-09-06T07:53:34Z</dc:date>
    </item>
    <item>
      <title>Re: extracting first few digits from a numeric variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extracting-first-few-digits-from-a-numeric-variable/m-p/184330#M265452</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi RichardInOz,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Sorry; going to have to reply to your post.&amp;nbsp; As far as I am aware there is no method of performing calculations on a character variable.&amp;nbsp; The data numeric vs character is very different at a binary level.&amp;nbsp;&amp;nbsp; Yes there are ,any clever solutions which may supress warnings, however behind the scenes they are all converting the character to numeric, then performing the calculation, then converting back again.&amp;nbsp; There is no way round it, characters need to be converted to numeric for mathmatical operations to be performed. Wether you are happy to let the machine decide when to do this is of course, up to you.&amp;nbsp; For my money I would want to see it, whats a simple input statement in the great scheme of things.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 06 Sep 2014 18:17:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extracting-first-few-digits-from-a-numeric-variable/m-p/184330#M265452</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2014-09-06T18:17:26Z</dc:date>
    </item>
    <item>
      <title>Re: extracting first few digits from a numeric variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extracting-first-few-digits-from-a-numeric-variable/m-p/184331#M265453</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Wot???&lt;/P&gt;&lt;P&gt;The formula is entirely numeric, intended to operate on a numeric variable, as described by the OP.&lt;/P&gt;&lt;P&gt;As it happens I am in agreement with you about automatic conversions.&lt;/P&gt;&lt;P&gt;Apology accepted&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Richard&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 07 Sep 2014 07:20:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extracting-first-few-digits-from-a-numeric-variable/m-p/184331#M265453</guid>
      <dc:creator>RichardinOz</dc:creator>
      <dc:date>2014-09-07T07:20:19Z</dc:date>
    </item>
    <item>
      <title>Re: extracting first few digits from a numeric variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extracting-first-few-digits-from-a-numeric-variable/m-p/184332#M265454</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;RichardinOz, Not all questions of some OP are making some sense. Trying to give technical answer will harm them more in the end. To which of the 10 persons do you belong?&lt;/P&gt;&lt;P&gt;Op's question wqas about the first few digits of the numeric. Please solve that in Roman notation: &lt;A href="http://support.sas.com/documentation/cdl/en/leforinforref/64790/HTML/default/viewer.htm#p0k2nghiwrkabdn1reagtomhohh1.htm" title="http://support.sas.com/documentation/cdl/en/leforinforref/64790/HTML/default/viewer.htm#p0k2nghiwrkabdn1reagtomhohh1.htm"&gt;SAS(R) 9.4 Formats and Informats: Reference&lt;/A&gt;. the question is humbug.&amp;nbsp; &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 07 Sep 2014 07:59:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extracting-first-few-digits-from-a-numeric-variable/m-p/184332#M265454</guid>
      <dc:creator>jakarman</dc:creator>
      <dc:date>2014-09-07T07:59:36Z</dc:date>
    </item>
    <item>
      <title>Re: extracting first few digits from a numeric variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extracting-first-few-digits-from-a-numeric-variable/m-p/184333#M265455</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi !&lt;/P&gt;&lt;P&gt;According to the documentation send by &lt;SPAN class="j-post-author "&gt;&lt;STRONG&gt;Jaap Karman, &lt;A href="http://support.sas.com/documentation/cdl/en/lrcon/67885/HTML/default/viewer.htm#p0ji1unv6thm0dn1gp4t01a1u0g6.htm" title="http://support.sas.com/documentation/cdl/en/lrcon/67885/HTML/default/viewer.htm#p0ji1unv6thm0dn1gp4t01a1u0g6.htm"&gt;SAS(R) 9.4 Language Reference: Concepts, Fourth Edition&lt;/A&gt;&lt;/STRONG&gt;&lt;/SPAN&gt;, we can read that:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;TABLE class="tgroup"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TH style="text-align: left;"&gt;&lt;P class="xis-paraTableFirst" id="n0qwk4shq0729in1uma28apysmtq"&gt;When Variable Length Equals ...&amp;nbsp; &lt;/P&gt;&lt;/TH&gt;&lt;TH class="xis-horizontalRight"&gt;&lt;P class="xis-paraTableFirst" id="p0nh56ojor12run1ekve1d6tc28o"&gt;Largest Integer&lt;/P&gt;&lt;P class="xis-paraTable" id="n1ld5m8x4fc6f4n10yhsqp8iirrw"&gt;&lt;SPAN class="xis-nobr"&gt;z/OS&lt;/SPAN&gt;&lt;/P&gt;&lt;/TH&gt;&lt;TH class="xis-horizontalRight"&gt;&lt;P class="xis-paraTableFirst" id="p0gpgfxgzxb0uan13joucwhrggni"&gt;Largest Integer&lt;/P&gt;&lt;P class="xis-paraTable" id="p005k67asic3sun16gusqzqkqe8v"&gt;Windows/UNIX&lt;/P&gt;&lt;/TH&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="text-align: center;"&gt;&lt;P class="xis-paraTableFirst" id="n17vxxx38psoaxn14k9de0p2tqk9"&gt;2&lt;/P&gt;&lt;/TD&gt;&lt;TD style="text-align: right;"&gt;&lt;P class="xis-paraTableFirst" id="p17lc8d869fj15n1lsoqy94osvs7"&gt;256&lt;/P&gt;&lt;/TD&gt;&lt;TD style="text-align: right;"&gt;&lt;P class="xis-paraTableFirst" id="p1ggrfpeeg9zxin1mxufmabfgfvy"&gt;not applicable&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="text-align: center;"&gt;&lt;P class="xis-paraTableFirst" id="p0syfrmtxvu07xn1jzayk3drqn3x"&gt; &lt;SPAN style="color: #ff00ff;"&gt;3&lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD style="text-align: right;"&gt;&lt;P class="xis-paraTableFirst" id="p00yzj3g7eo8qyn1bdsad86dvy97"&gt; 65,536&amp;nbsp; &lt;/P&gt;&lt;/TD&gt;&lt;TD style="text-align: right;"&gt;&lt;P class="xis-paraTableFirst" id="n13x7swxk0158hn1t34hkm6fk7tq"&gt;8,192 &lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="text-align: center;"&gt;&lt;P class="xis-paraTableFirst" id="n07iyon2094htsn19s8kchep3zks"&gt; &lt;SPAN style="color: #00ccff;"&gt;4 &lt;/SPAN&gt;&lt;/P&gt;&lt;/TD&gt;&lt;TD style="text-align: right;"&gt;&lt;P class="xis-paraTableFirst" id="n1acuzuoljipxfn1m5g2z88g5ulo"&gt;16,777,216 &lt;/P&gt;&lt;/TD&gt;&lt;TD style="text-align: right;"&gt;&lt;P class="xis-paraTableFirst" id="p0qxtset0lt5xcn11jliqrs7tlnb"&gt;2,097,152 &lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="text-align: center;"&gt;&lt;P class="xis-paraTableFirst" id="n1p0rnjudfk4rxn1lwhli0jr2xec"&gt;5 &lt;/P&gt;&lt;/TD&gt;&lt;TD style="text-align: right;"&gt;&lt;P class="xis-paraTableFirst" id="p0gd6y9nyy7m70n10bzt5ap6keud"&gt;4,294,967,296 &lt;/P&gt;&lt;/TD&gt;&lt;TD style="text-align: right;"&gt;&lt;P class="xis-paraTableFirst" id="n187v6gtnjwkxfn1bg60t0k2e169"&gt;536,870,912 &lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="text-align: center;"&gt;&lt;P class="xis-paraTableFirst" id="n1u295g0y06z4wn1gpmnzgw7i7i8"&gt;6 &lt;/P&gt;&lt;/TD&gt;&lt;TD style="text-align: right;"&gt;&lt;P class="xis-paraTableFirst" id="n1te184zb13yxpn1je1hi452ajat"&gt;1,099,511,627,776&lt;/P&gt;&lt;/TD&gt;&lt;TD style="text-align: right;"&gt;&lt;P class="xis-paraTableFirst" id="p0eugjc1efm8wbn1ama7vdu1onpm"&gt;137,438,953,472 &lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="text-align: center;"&gt;&lt;P class="xis-paraTableFirst" id="p00y9dkqnn92gnn1j1r664d86e0l"&gt;7 &lt;/P&gt;&lt;/TD&gt;&lt;TD style="text-align: right;"&gt;&lt;P class="xis-paraTableFirst" id="p1us8qjqc3hd4yn15qqw3rc8pc5r"&gt;281,474,946,710,656 &lt;/P&gt;&lt;/TD&gt;&lt;TD style="text-align: right;"&gt;&lt;P class="xis-paraTableFirst" id="n1ixmxgf8rt2z3n1w35ww7irgk7f"&gt;35,184,372,088,832&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD style="text-align: center;"&gt;&lt;P class="xis-paraTableFirst" id="n07j8jg68z1pujn1eaj6e9p6kirc"&gt;8 (default)&lt;/P&gt;&lt;/TD&gt;&lt;TD style="text-align: right;"&gt;&lt;P class="xis-paraTableFirst" id="n1hht28dqr0cnln17rshr44qmr5i"&gt;72,057,594,037,927,936&lt;/P&gt;&lt;/TD&gt;&lt;TD style="text-align: right;"&gt;&lt;P class="xis-paraTableFirst" id="n14rv9qgabmcbwn1xl5vb617q7cb"&gt;9,007,199,254,740,992 &lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;When viewing this table, consider the following points:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="color: #ff00ff;"&gt;&lt;STRONG&gt;The minimum length&lt;/STRONG&gt;&lt;/SPAN&gt; for a SAS variable on Windows and UNIX operating systems &lt;SPAN style="color: #ff00ff;"&gt;&lt;STRONG&gt;is 3 bytes&lt;/STRONG&gt;,&lt;/SPAN&gt; and the maximum length is 8 bytes&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;But we know that the lenght of a numeric variable affects only the output of that variable. So to extract the first digits using substr() function, you have to put the beginning position to 1 and the end position (count from the first digit to the last one to conder where the first digit represent 3 or 4 according to the numeric to treat)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Exemples:&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;a = 123456789;&lt;/P&gt;&lt;P&gt;phonenum = 3125551212 ; &lt;/P&gt;&lt;P&gt;b=substr(a,1,7);&amp;nbsp; /* to get 7, the first digit begin at 4*/&lt;/P&gt;&lt;P&gt;c=substr(a,1,11);&lt;/P&gt;&lt;P&gt;mim=substr(phonenum,1,12); /*to get 12, the first digit begin at 3 */&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;proc print data=want;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;IMG alt="numsas.png" class="jive-image" src="https://communities.sas.com/legacyfs/online/8936_numsas.png" /&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 01 Feb 2015 10:50:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extracting-first-few-digits-from-a-numeric-variable/m-p/184333#M265455</guid>
      <dc:creator>mmabika</dc:creator>
      <dc:date>2015-02-01T10:50:12Z</dc:date>
    </item>
    <item>
      <title>Re: extracting first few digits from a numeric variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/extracting-first-few-digits-from-a-numeric-variable/m-p/184334#M265456</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Sounds you are mixing data storage rules (the table are visualizing no of numerical bytes and integer accuracy).&lt;/P&gt;&lt;P&gt;To do sub-strings, you are probably in to look at numerical values as humans see them, in it's decimal form.&lt;/P&gt;&lt;P&gt;Sometime SAS is nice to us and let us do string operations on numerical values, but best practice is to put a numerical value using a format, before attempt to use any string operations. This is similar to CAST in other SQL based DBMS.&lt;/P&gt;&lt;P&gt;Whether your examples work or not depends on the length of the char variables b, c and mim - quite risky. Risks should be minimized in your programming.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 01 Feb 2015 12:57:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/extracting-first-few-digits-from-a-numeric-variable/m-p/184334#M265456</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2015-02-01T12:57:28Z</dc:date>
    </item>
  </channel>
</rss>

