BookmarkSubscribeRSS Feed
rajd1
Quartz | Level 8

Hi all,

I have a character variable that needs to be converted to numeric. The character <0.001 needs to be converted to 0.001 numeric value.

Data Have:

data have;
input iD nums $;
cards;
1	1.675	
2	<0.001	
3	3.578	
4	<0.00001
;
5 REPLIES 5
Reeza
Super User

Would you also have cases of >= or <= or other symbols that need to be accounted for in this situation?

 


@rajd1 wrote:

Hi all,

I have a character variable that needs to be converted to numeric. The character <0.001 needs to be converted to 0.001 numeric value.

Data Have:

data have;
input iD nums $;
cards;
1	1.675	
2	<0.001	
3	3.578	
4	<0.00001
;

 

rajd1
Quartz | Level 8

@Reeza :

Yes, there might be < or > <= or >=

Reeza
Super User
Always at the beginning?
Patrick
Opal | Level 21

What will work depends on your data. If you're sure that there can't be numbers in scientific notation then the derivation syntax for nums_new_1 should be suitable. 

data have;
  input iD nums $;
  cards;
1 1.675 
2 <0.001  
3 3.578 
4 <0.00001
;

data want;
  set have;
  nums_new_1=input(compress(nums,'.+-','kd'),best32.);
  nums_new_2=input(compress(nums,'<=> '),best32.);
run;

 

 

ballardw
Super User

@Reeza wrote:

Would you also have cases of >= or <= or other symbols that need to be accounted for in this situation?

 


@rajd1 wrote:

Hi all,

I have a character variable that needs to be converted to numeric. The character <0.001 needs to be converted to 0.001 numeric value.

Data Have:

data have;
input iD nums $;
cards;
1	1.675	
2	<0.001	
3	3.578	
4	<0.00001
;

 


One way might be:

data want;
  set have;
  newnum=input(compress(nums, "+-.", "kd"),12.);
run;

The compress function removes everything except the + - decimal point and digits (the d option indicates digits, the k keeps instead of removing. Then input converts to numeric. This assumes you don't have more than 12 characters in the resulting numeric value string.

Warning: if for some reason you have exponential notation values such as 1.5E3 in the data the E gets removed and the result would be a missing value. If that is the case you need to check for the presence of the E and treat that string differently (add E to the list of characters "+-.eE" for example)

sas-innovate-wordmark-2025-midnight.png

Register Today!

Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.


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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 1015 views
  • 2 likes
  • 4 in conversation