BookmarkSubscribeRSS Feed
SASPhile
Quartz | Level 8
How to convert the values to numeric,at times there will be a comma sometimes there will not be a comma.
29,652 units
60mg
10 REPLIES 10
Peter_C
Rhodochrosite | Level 12
informat COMMAw. will deal with embedded commas correctly, but you might have a problem with "mg" tight against the numbers.

PeterC
Flip
Fluorite | Level 6
z = input( prxchange('s/[A-Z a-z]/ /',-1, x), comma6.);
SASPhile
Quartz | Level 8
Thanks Flip.
SASPhile
Quartz | Level 8
Thanks Peter
SASPhile
Quartz | Level 8
Thanks Flip.
marieK
Obsidian | Level 7
Hi,

I have the same problem. I want to convert a character-Variable into a numeric. But the tip above doesnt work correctly in my case.

My data is like (character-format):

0,8
1,2
12,3
10
1,2
.
.
.
after converting i want my data like (numeric-format):
0.8
1.2
12.3
10
1.2
.
.
.
if i do like above i get:
8
12
123
10
12
.
.
.

Any idea?

Thank you in advance. Marie Message was edited by: marieK
Patrick
Opal | Level 21
Use informat commaX.

data have;
input varHave $;
datalines;
0,8
1,2
12,3
10
1,2
;

data want;
set have;
varWant=input(varHave,commax8.);
put varWant=;
run;
marieK
Obsidian | Level 7
Juhuuu 🙂 thanks!
Ksharp
Super User
Hi.
For the first question, you can use function translate() to convert ',' into '.',then use input() to get the numeric value.
[pre]
data _null_;
infile datalines;
input char $;
num= translate(char,'.',',');
put num=;
datalines;
0,8
1,2
12,3
10
1,2
;
run;
[/pre]
Note: num is also characteric value.

For the second question,you can use compress() to get rid of comma.then can achieve the numeric value.
[pre]
data _null_;
infile datalines;
input char $;
num= compress(char,',');
put num=;
datalines;
0,8
1,2
12,3
10
1,2
;
run;
[/pre]
Note: num is also characteric value.


Ksharp
marieK
Obsidian | Level 7
Thank you! The translate-function is good 🙂

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 10 replies
  • 2904 views
  • 0 likes
  • 6 in conversation