Solved
Contributor
Posts: 36

Extracting the first non-zero digit from a numeric variable

Hi all,

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 numbers in my datasets (more than 150 variables * more than 10,000 rows). Please find the attachment for example of wanted variable.

Best Regards,

Tri

Accepted Solutions
Solution
‎07-30-2016 09:05 AM
Contributor
Posts: 47

Re: Extracting the first non-zero digit from a numeric variable

[ Edited ]
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;
/* 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 */

All Replies
Super User
Posts: 13,583

Re: Extracting the first non-zero digit from a numeric variable

What is the desired result if the value is exactly 0?

Here's one way, without implementing a result for 0;

data want;
input x;
y = substr(strip(compress(put(x,best32.),'0-.')),1,1);
datalines;
0.1
-0.008
1234
-456.98
;
run;
Contributor
Posts: 36

Re: Extracting the first non-zero digit from a numeric variable

Solution
‎07-30-2016 09:05 AM
Contributor
Posts: 47

Re: Extracting the first non-zero digit from a numeric variable

[ Edited ]
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;
/* 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 */

Contributor
Posts: 36

Re: Extracting the first non-zero digit from a numeric variable

Thank you very much for your response and explanation! I found that your code works for me. Many thanks! Tri

Posts: 5,541

Re: Extracting the first non-zero digit from a numeric variable

Mathematically,

if x = 0 then firstDigit = 0;

else firstDigit = floor(10**(log10(abs(x)) - floor(log10(abs(x)))));

beyond mathematical concepts, coding is explained in the SAS documentation of log10, floor and abs functions.

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;
PG
Posts: 3,167

Re: Extracting the first non-zero digit from a numeric variable

Or PRX:

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;
Super User
Posts: 10,787

Re: Extracting the first non-zero digit from a numeric variable

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;

☑ This topic is solved.