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.
Thank you very much for your help. Please explain your coding when possible. Thanks!
Best Regards,
Tri
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 */
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;
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 */
Thank you very much for your response and explanation! I found that your code works for me. Many thanks! Tri
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;
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;
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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.