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

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?

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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

4 REPLIES 4
Reeza
Super User

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.);
Ksharp
Super User

Canyou post some data?

 

Var_Num = input(var_char, ??  best12.);
Patrick
Opal | Level 21

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;
Ksharp
Super User

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;

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 connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

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