BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
tritringuyen
Quartz | Level 8

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


want.png
1 ACCEPTED SOLUTION

Accepted Solutions
Elle
Quartz | Level 8
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 */ 

 

View solution in original post

7 REPLIES 7
ballardw
Super User

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;
tritringuyen
Quartz | Level 8
Thanks for your support!
Elle
Quartz | Level 8
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 */ 

 

tritringuyen
Quartz | Level 8

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

PGStats
Opal | Level 21

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
Haikuo
Onyx | Level 15

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;
Ksharp
Super User
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;


sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 3494 views
  • 2 likes
  • 6 in conversation