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 🙂

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 1277 views
  • 0 likes
  • 6 in conversation