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;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.