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?
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.);
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.);
Canyou post some data?
Var_Num = input(var_char, ?? best12.);
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;
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;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.