SAS Data Integration Studio, DataFlux Data Management Studio, SAS/ACCESS, SAS Data Loader for Hadoop and others

Changing character values to numeric values

Accepted Solution Solved
Reply
New Contributor
Posts: 4
Accepted Solution

Changing character values to numeric values

I have a SAS dataset with a variable called LENGTH, which describe the length of a fishing line.  But some of the values have a ',' instead of a '.' to designate a decimal point, so SAS read this variable as a character variable.  How do I replace all ',' with '.' and convert the variable to numeric?


Accepted Solutions
Solution
‎10-25-2016 04:53 PM
Super User
Posts: 17,963

Re: Changing character values to numeric values

Use TRANSLATE to replace the commas with periods.

var_char = translate(var_char, ".", ",");

 

USE INPUT to convert character to numeric 

Var_Num = input(var_char, best12.);

View solution in original post


All Replies
Solution
‎10-25-2016 04:53 PM
Super User
Posts: 17,963

Re: Changing character values to numeric values

Use TRANSLATE to replace the commas with periods.

var_char = translate(var_char, ".", ",");

 

USE INPUT to convert character to numeric 

Var_Num = input(var_char, best12.);
Super User
Posts: 9,691

Re: Changing character values to numeric values

Canyou post some data?

 

Var_Num = input(var_char, ??  best12.);
Respected Advisor
Posts: 3,908

Re: Changing character values to numeric values

If you're reading the data from a text file then you could also do something like below:

proc format;
  invalue mixedComma
    low-high = [comma.]
    other    = [commax.]
  ;
quit;

data test;
  infile datalines dsd dlm=' ' truncover;
  input var :mixedComma.;
  datalines;
10.2
10,2
10
;
run;
Super User
Posts: 9,691

Re: Changing character values to numeric values

Patrick,

I would do this.

 

data test;
  infile datalines dsd dlm=' ' truncover;
  input var $;
  if find(var,',') then fmt='commax32.';
   else fmt='best32.    ';
  new=inputn(var,fmt);
  datalines;
10.2
10,2
10
;
run;
☑ This topic is solved.

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

Discussion stats
  • 4 replies
  • 415 views
  • 2 likes
  • 4 in conversation