SAS Examples: Numeric/Character Conversions

SAS Examples: Numeric/Character Conversions

Last updated:
Table of Contents

Convert char to numeric

proc sql;
    select input(<tbl_alias>.<char_field>,8.)
    from <namespace>.<full_table_name> as <tbl_alias>;
quit;

Convert numeric to char

proc sql;
    select put(<numeric-attribute>,7.)
    from <table>
    where <conditions>;
quit;

Pad numeric value with leading zeros

Suppose <my_numeric_value> is a numeric SAS value. This will convert it to char and pad the value.

translate(right(put(<my_numeric_value>,9.0)),'0',' ')

Restrict decimal places in Proc SQL

Use format X.Y where X indicates the total number of digits and Y indicates the number of decimal places.

data sandbox.tests;
    length name $20.;
    input id name $ balance; 

DATALINES;
1 bob   1.12  
2 john  2.312321
3 peter 3.1416
;
run;
id name balance
1 bob 1.12
2 john 2.312321
3 peter 3.1416
proc sql;
    select 
        balance format 9.2
    from sandbox.tests;
quit;
balance
1.12
2.31
3.14

References

Dialogue & Discussion