SAS Examples: Numeric/Character Conversions

Last updated:

WIP Alert This is a work in progress. Current information is correct but more content may be added in the future.

Convert a character value to a numeric value

proc sql;
    select input(<tbl_alias>.<char_field>,8.)
    from <namespace>.<full_table_name> as <tbl_alias>;
quit;
Twitter Linkedin YC Hacker News Reddit

Convert a numeric value to a character value

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

Pad a numeric value with leading zeros (as a character value)

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