DATA Step, Macro, Functions and more

converting character values to numeric values

Accepted Solution Solved
Reply
Frequent Contributor
Frequent Contributor
Posts: 76
Accepted Solution

converting character values to numeric values

Hello, 

 

 

May you help with following converting the characer value with decimal into numeric value: 

 

test = input( price, best12.) , it did not work.

 

Price:  Format $7, informat $7.  

example of price :  6.32,  0.11,  0.44, 11.73

 

Thank you.

Ivy

 

 

 

 

 


Accepted Solutions
Solution
‎03-29-2017 09:25 PM
PROC Star
Posts: 7,360

Re: converting character values to numeric values

Works for me. I'd have to think that you entered something wrong (e.g., missing semi-colon or the like).

 

The following works:

 

data have;
  format  price $7.;
  informat price $7.;
  input price;
  cards;
6.32
0.11
0.44
11.73
;

data want;
  set have;
  test = input( price, best12.);
run;

Art, CEO, AnalystFinder.com

 

View solution in original post


All Replies
Solution
‎03-29-2017 09:25 PM
PROC Star
Posts: 7,360

Re: converting character values to numeric values

Works for me. I'd have to think that you entered something wrong (e.g., missing semi-colon or the like).

 

The following works:

 

data have;
  format  price $7.;
  informat price $7.;
  input price;
  cards;
6.32
0.11
0.44
11.73
;

data want;
  set have;
  test = input( price, best12.);
run;

Art, CEO, AnalystFinder.com

 

Frequent Contributor
Frequent Contributor
Posts: 76

Re: converting character values to numeric values

Thank you , the special non-printing character in the column made that did not work.
Super User
Super User
Posts: 6,499

Re: converting character values to numeric values

Your example statement should work.  Make sure that your text string does not have non-printing characters like tabs, carriage returns or non-breaking spaces ('09'x,'0A'x,'A0'x, respectively).

 

data want ;
  length price $7 ;
  input price ;
  test = input(price, 7.);
cards;
6.32
0.11
0.44
11.73
;;;;

A couple of notes.

  • The LENGTH of a character variable is more important than what FORMAT or INFORMAT is attached to it.  In fact the FORMAT can actually cause trouble if it doesn't match the length since it can make it hard to debug.
  • BEST is really a FORMAT and not an INFORMAT.  SAS will just use the 12. INFORMAT if you ask it to use BEST12. as in informat. Actually for reading text into numbers the COMMA informat is good one to use because it will ignore dollar signs and commas in your strings.  

To see if your strings have non-printing characters print the value using $HEX format. '20'X is the code for a space.

Or you the COMPRESS() function to remove non digits.  Something like this might help.

test = input(COMPRESS(price, "+.-", "d"),32.);

 

 

Frequent Contributor
Frequent Contributor
Posts: 76

Re: converting character values to numeric values

Thank you very much. There is special character in the column which made it did not work.
Super User
Posts: 10,483

Re: converting character values to numeric values

Doesn't work is awful vague.

Are there errors in the log: Post the code and log in a code box opened with the {i} to maintain formatting of error messages.

No output? Post any log in a code box.


Unexpected output? Provide input data in the form of a dataset, the actual results and the expected results. Data should be in the form of a data step. Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.

 

Also is your value for Price= '6.32' or '$6.32'? or does your Price have values separated by commas? Leading spaces? It isn't quite clear from your example. 

 

 

Frequent Contributor
Frequent Contributor
Posts: 76

Re: converting character values to numeric values

Thank you !
☑ This topic is SOLVED.

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

Discussion stats
  • 6 replies
  • 139 views
  • 0 likes
  • 4 in conversation