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

I've a value as '0,50708683901377' character and I want to convert it to numeric. After numeric conversion, I want the value as same as before I convert.

I tried with the Format 21.4 and also NUMX18.10 but it's not producing the desired results. Any help with the Format to produce the desired value?

 

What I tried is,

 

(input(H,Best32.)) as Value length = 8
            format = 21.4
            informat = 21.4

Desired result is, 0,50708683901377 as numeric

 

1 ACCEPTED SOLUTION

Accepted Solutions
DanielRingqvist
SAS Employee
I see that CommaX will work just as well as NumX.

From the doc
"The COMMAX w. d informat reads numeric values and removes embedded periods, blanks, dollar signs, percent signs, hyphens, and close parentheses from the input data. The COMMAX w. d informat converts an open parenthesis at the beginning of a field to a minus sign"

View solution in original post

10 REPLIES 10
Shmuel
Garnet | Level 18

It seems that the comma inside the number is for decimal sign (0,5 = a half).

in your case the number = '0,50708683901377'  has 14 decimal digits.

Try using format 21.14 

David_Billa
Rhodochrosite | Level 12
Yes I have a comma inside number. Should I use 21.14 instead of 21.14 as I
have 14 numbers after comma?

Please help me understand this format.
Shmuel
Garnet | Level 18

21.14 means 21 characters in whole, including the comma with 14 decimal digits, which leaves 6 characters for the integer. (6+1+14 = 21).

hhinohar
Quartz | Level 8
/* https://documentation.sas.com/?cdcId=vdmmlcdc&cdcVersion=8.1&docsetId=leforinforref&docsetTarget=n056hzkipsxpcbn11ww9i9i47l0h.htm&locale=en */
data test_comma;
	format h commax16.14;
	h=input('0,50708683901377',comma16.14);
	put h=;
run;
DanielRingqvist
SAS Employee

As you have a decimal comma you need to use the "X" formats (commax, numx etc.).

 

In this case use NumX32.14.

Even though NumX16.14 would be enough to handle the width of this string the next value could be '65342522,50708683901377' which would require NumX23.14. 

 

 

Data char2num;
	H = '0,50708683901377';
	Value = Input(H,numX32.14);
	Format Value NumX32.14;
	Put Value NumX32.14 ;
Run;
David_Billa
Rhodochrosite | Level 12
Can we use commx32.14 format also?
DanielRingqvist
SAS Employee
Yes, in this case.

But if your input value was '65342522,50708683901377' it wouldn't work as CommaX looks for thousand separators.

With '65.342.522,50708683901377' you should use CommaX.
David_Billa
Rhodochrosite | Level 12
Well, NumX formats can also be used in place of CommaX format?
DanielRingqvist
SAS Employee
I see that CommaX will work just as well as NumX.

From the doc
"The COMMAX w. d informat reads numeric values and removes embedded periods, blanks, dollar signs, percent signs, hyphens, and close parentheses from the input data. The COMMAX w. d informat converts an open parenthesis at the beginning of a field to a minus sign"
Kurt_Bremser
Super User

Do not use fractional parts in informats. If no comma is present in the string, the informat will first divide by 10**(fractional part), and then apply the format. A string of "1" would then result in 1e-14.

Simply do

input(H,commax32.) as Value length = 8 format = 21.4

it is not necessary to define an informat; defined informats are only needed when reading from external data.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 10 replies
  • 1789 views
  • 7 likes
  • 5 in conversation