DATA Step, Macro, Functions and more

Convert text string to numeric

Accepted Solution Solved
Reply
New Contributor
Posts: 3
Accepted Solution

Convert text string to numeric

Hi there

I'm trying to convert a character string - which is in fact numeric, into numeric - 0.74144, 3.533, 4.578612

I've tried SLOPE_DEG = INPUT(SLOPE_DEG,6.4); but this just returns empty spaces.

Any ideas?

Thanks

Mat


Accepted Solutions
Solution
‎10-13-2016 02:40 AM
Super User
Posts: 7,809

Re: Convert text string to numeric

[ Edited ]

If you convert character to numeric, you must create a new variable. SLOPE_DEG is of type character and will stay type character.

data have;
input slope_deg $;
cards;
0.74144
3.533
4.578612
;
run;

data want;
set have;
slope_deg_num = input(slope_deg,best.);
run;

proc print;run;

returns this output:

       slope_       slope_
Obs    deg         deg_num

 1     0.74144     0.74144
 2     3.533       3.53300
 3     4.578612    4.57861
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Solution
‎10-13-2016 02:40 AM
Super User
Posts: 7,809

Re: Convert text string to numeric

[ Edited ]

If you convert character to numeric, you must create a new variable. SLOPE_DEG is of type character and will stay type character.

data have;
input slope_deg $;
cards;
0.74144
3.533
4.578612
;
run;

data want;
set have;
slope_deg_num = input(slope_deg,best.);
run;

proc print;run;

returns this output:

       slope_       slope_
Obs    deg         deg_num

 1     0.74144     0.74144
 2     3.533       3.53300
 3     4.578612    4.57861
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
New Contributor
Posts: 3

Re: Convert text string to numeric

Posted in reply to KurtBremser
Thanks so much, fast reply!
Super User
Posts: 19,822

Re: Convert text string to numeric

Use best12. Or best32 as your informat. 

 

Setting it to 6.4 tells SAS to look for an number that has 5 digits + 1 decimal point for a width of 6 and 4 of those 5 digits are after the decimal point. That's not your format based on the samples you've posted. . 

New Contributor
Posts: 3

Re: Convert text string to numeric

Thanks, awesome help!
☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 399 views
  • 5 likes
  • 3 in conversation