BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Ivy
Quartz | Level 8 Ivy
Quartz | Level 8

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

 

 

 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
art297
Opal | Level 21

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

6 REPLIES 6
art297
Opal | Level 21

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

 

Ivy
Quartz | Level 8 Ivy
Quartz | Level 8
Thank you , the special non-printing character in the column made that did not work.
Tom
Super User Tom
Super User

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.);

 

 

Ivy
Quartz | Level 8 Ivy
Quartz | Level 8
Thank you very much. There is special character in the column which made it did not work.
ballardw
Super User

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. 

 

 

Ivy
Quartz | Level 8 Ivy
Quartz | Level 8
Thank you !

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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
  • 6 replies
  • 8162 views
  • 0 likes
  • 4 in conversation