DATA Step, Macro, Functions and more

How to convert the character string including percentage and numbers into numeric data?

Accepted Solution Solved
Reply
Contributor
Posts: 62
Accepted Solution

How to convert the character string including percentage and numbers into numeric data?

Hello

I have character strings which including percentage and numbers. how do I convert them into numeric data?

my data is 

var

45.5%

34

23.64%

23

44.56%

798

 

I use the code:

data a;

set a;

if var=:"%" then var1=input(var,percent8.);

else var1=input(var,best8.);

run;

But it does not work.

Thanks a lot!


Accepted Solutions
Solution
‎06-16-2017 09:50 PM
Trusted Advisor
Posts: 1,137

Re: How to convert the character string including percentage and numbers into numeric data?

I tried as below and it works

 

data have;
input var$10.;;
cards;
45.5%
34
23.64%
23
44.56%
798
;

data want;
set have;
if index(var,'%') then new=input(var,percent.);
else new=input(var,best.);
run;
Thanks,
Jag

View solution in original post


All Replies
Solution
‎06-16-2017 09:50 PM
Trusted Advisor
Posts: 1,137

Re: How to convert the character string including percentage and numbers into numeric data?

I tried as below and it works

 

data have;
input var$10.;;
cards;
45.5%
34
23.64%
23
44.56%
798
;

data want;
set have;
if index(var,'%') then new=input(var,percent.);
else new=input(var,best.);
run;
Thanks,
Jag
Contributor
Posts: 62

Re: How to convert the character string including percentage and numbers into numeric data?

Posted in reply to Jagadishkatam
Thank you very much!
Super User
Posts: 19,855

Re: How to convert the character string including percentage and numbers into numeric data?

The =: only checks if the variables start with that character string, not contains. ie does it start with a percentage sign. To check if hte variable contains a % sign you would use FIND or INDEX. 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 230 views
  • 2 likes
  • 3 in conversation