BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
HitmonTran
Pyrite | Level 9

Hello,

 

How do i extract the value in the "( )" and convert into numeric? I only want numbers, not "NA".

data:

var1 (character)

3 ( na   )
 81 (41.1%)
3 (1.5%)

 

want:

var2 (numeric)

.
41.1
1.5
1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

Untested because example data not provided in form of working data step code.

data want;
   set have;
   var2 = input(scan(var1,2,'()'),?? comma12.);
   format var2 percent8.1;
run;

The '()' in the SCAN function is telling the function to only consider ( and ) when separating out values. So it is easier to tell exactly where/end the desired second value occurs.

If you want to change the actual value from 41.1% then you need to add a multiply by 100. I left the value alone and provided a format that will display the digits in the expected range. The ?? preceding the informat comma12 is to suppress the invalid data messages when attempting to convert "na" into a numeric value.

 


@HitmonTran wrote:

Hello,

 

How do i extract the value in the "( )" and convert into numeric? I only want numbers, not "NA".

data:

var1 (character)

3 ( na   )
 81 (41.1%)
3 (1.5%)

 

want:

var2 (numeric)

.
41.1
1.5

 

View solution in original post

1 REPLY 1
ballardw
Super User

Untested because example data not provided in form of working data step code.

data want;
   set have;
   var2 = input(scan(var1,2,'()'),?? comma12.);
   format var2 percent8.1;
run;

The '()' in the SCAN function is telling the function to only consider ( and ) when separating out values. So it is easier to tell exactly where/end the desired second value occurs.

If you want to change the actual value from 41.1% then you need to add a multiply by 100. I left the value alone and provided a format that will display the digits in the expected range. The ?? preceding the informat comma12 is to suppress the invalid data messages when attempting to convert "na" into a numeric value.

 


@HitmonTran wrote:

Hello,

 

How do i extract the value in the "( )" and convert into numeric? I only want numbers, not "NA".

data:

var1 (character)

3 ( na   )
 81 (41.1%)
3 (1.5%)

 

want:

var2 (numeric)

.
41.1
1.5

 

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
  • 1 reply
  • 169 views
  • 0 likes
  • 2 in conversation