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)

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 5 replies
  • 584 views
  • 2 likes
  • 4 in conversation