<?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 Extracting the first non-zero digit from a numeric variable in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Extracting-the-first-non-zero-digit-from-a-numeric-variable/m-p/287527#M59181</link>
    <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a problem with my dataset. From given variables (numberic), I want the first non-zero digit of positive numbers, negative numbers and decimals. Unfortunately, a variable may have any of those numbers. I may have million&amp;nbsp;numbers in my datasets (more than 150 variables * more than 10,000 rows). Please find the attachment for example of wanted variable.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you very much for your help. Please explain your coding when possible. Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Best Regards,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Tri&lt;/P&gt;&lt;BR /&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/12785iCC8E1DC13C68C17A/image-size/large?v=1.0&amp;amp;px=600" border="0" alt="want.png" title="want.png" /&gt;</description>
    <pubDate>Wed, 27 Jul 2016 16:51:37 GMT</pubDate>
    <dc:creator>tritringuyen</dc:creator>
    <dc:date>2016-07-27T16:51:37Z</dc:date>
    <item>
      <title>Extracting the first non-zero digit from a numeric variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-the-first-non-zero-digit-from-a-numeric-variable/m-p/287527#M59181</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a problem with my dataset. From given variables (numberic), I want the first non-zero digit of positive numbers, negative numbers and decimals. Unfortunately, a variable may have any of those numbers. I may have million&amp;nbsp;numbers in my datasets (more than 150 variables * more than 10,000 rows). Please find the attachment for example of wanted variable.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you very much for your help. Please explain your coding when possible. Thanks!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Best Regards,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Tri&lt;/P&gt;&lt;BR /&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/12785iCC8E1DC13C68C17A/image-size/large?v=1.0&amp;amp;px=600" border="0" alt="want.png" title="want.png" /&gt;</description>
      <pubDate>Wed, 27 Jul 2016 16:51:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-the-first-non-zero-digit-from-a-numeric-variable/m-p/287527#M59181</guid>
      <dc:creator>tritringuyen</dc:creator>
      <dc:date>2016-07-27T16:51:37Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting the first non-zero digit from a numeric variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-the-first-non-zero-digit-from-a-numeric-variable/m-p/287534#M59182</link>
      <description>&lt;P&gt;What is the desired&amp;nbsp;result if the value is exactly 0?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's one way, without implementing a result for 0;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
   input x;
   y = substr(strip(compress(put(x,best32.),'0-.')),1,1);
datalines;
0.1
-0.008
1234
-456.98
;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 27 Jul 2016 17:32:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-the-first-non-zero-digit-from-a-numeric-variable/m-p/287534#M59182</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2016-07-27T17:32:26Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting the first non-zero digit from a numeric variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-the-first-non-zero-digit-from-a-numeric-variable/m-p/287540#M59185</link>
      <description>&lt;PRE&gt;data have;
	input numvar;
	datalines;
89
-56
0.89
0.007
-0.008
;run;

data want;
	set have;
	wantvar=substr(compress(numvar, "+-0., "), 1, 1)*1;
run;&lt;/PRE&gt;&lt;PRE&gt;        /* COMPRESS function eliminates all characters in quotes from your variable */
	/* SUBSTR function extracts characters from a position of 1, of a length of 1 */
	/* Since SUBSTR is a character function it turn the variable into character, so to make it numeric again I just multiply by 1 */ &lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 27 Jul 2016 20:46:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-the-first-non-zero-digit-from-a-numeric-variable/m-p/287540#M59185</guid>
      <dc:creator>Elle</dc:creator>
      <dc:date>2016-07-27T20:46:05Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting the first non-zero digit from a numeric variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-the-first-non-zero-digit-from-a-numeric-variable/m-p/287541#M59186</link>
      <description>&lt;P&gt;Mathematically,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if x = 0 then firstDigit = 0;&lt;/P&gt;
&lt;P&gt;else firstDigit = floor(10**(log10(abs(x)) - floor(log10(abs(x)))));&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;beyond mathematical concepts, coding is explained in the SAS documentation of log10, floor and abs functions.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
do x = -123545, -2.34, -0.34567, 0, 0.00004567, 0.5678, 6.789, 789.012, 8.901e12;
    if x = 0 then firstDigit = 0;
    else firstDigit = floor(10**(log10(abs(x)) - floor(log10(abs(x)))));
    output;
    end;
format x e10.;
run;

proc print; run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 27 Jul 2016 17:44:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-the-first-non-zero-digit-from-a-numeric-variable/m-p/287541#M59186</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-07-27T17:44:24Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting the first non-zero digit from a numeric variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-the-first-non-zero-digit-from-a-numeric-variable/m-p/287576#M59194</link>
      <description>&lt;P&gt;Or PRX:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	input var1;
	cards;
89467
-5678944
0.89776
0.000677755
-0.0023456
;
run;

data want;
	set have;
	var_c=put(var1,best.);
	first_d = prxchange('s/([^1-9]*)([1-9])(.*)/$2/io',-1,var_c);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 27 Jul 2016 19:35:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-the-first-non-zero-digit-from-a-numeric-variable/m-p/287576#M59194</guid>
      <dc:creator>Haikuo</dc:creator>
      <dc:date>2016-07-27T19:35:12Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting the first non-zero digit from a numeric variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-the-first-non-zero-digit-from-a-numeric-variable/m-p/287703#M59224</link>
      <description>&lt;PRE&gt;
data want;
   input x;
   temp=put(x,best32.);
   y = substr(temp,prxmatch('/[1-9]/',temp),1);
   drop temp;
datalines;
0.1
-0.008
1234
-456.98
;
run;


&lt;/PRE&gt;</description>
      <pubDate>Thu, 28 Jul 2016 01:55:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-the-first-non-zero-digit-from-a-numeric-variable/m-p/287703#M59224</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-07-28T01:55:32Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting the first non-zero digit from a numeric variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-the-first-non-zero-digit-from-a-numeric-variable/m-p/288325#M59430</link>
      <description>Thanks for your support!</description>
      <pubDate>Sat, 30 Jul 2016 13:04:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-the-first-non-zero-digit-from-a-numeric-variable/m-p/288325#M59430</guid>
      <dc:creator>tritringuyen</dc:creator>
      <dc:date>2016-07-30T13:04:47Z</dc:date>
    </item>
    <item>
      <title>Re: Extracting the first non-zero digit from a numeric variable</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Extracting-the-first-non-zero-digit-from-a-numeric-variable/m-p/288326#M59431</link>
      <description>&lt;P&gt;Thank you very much for your response and explanation! I found that your code works for me. Many thanks! Tri&lt;/P&gt;</description>
      <pubDate>Sat, 30 Jul 2016 13:07:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Extracting-the-first-non-zero-digit-from-a-numeric-variable/m-p/288326#M59431</guid>
      <dc:creator>tritringuyen</dc:creator>
      <dc:date>2016-07-30T13:07:20Z</dc:date>
    </item>
  </channel>
</rss>

