adeline
adeline

Reputation: 21

Using PROC FORMAT and PROC FCMP to convert values in SAS

I nedd to write a PROC FORMAT and PROC FCMPin my SAS programm to convert values in horsepower to watts. Watts should have the following format: XXXXX,XX Watt.

proc fcmp outlib=work.functions.fun;
    function hptow(c) $;
        n = cats(c*745.7, ' Watts');
        return (n);
    endsub;
run;

options cmplib=work.functions;

proc format;
    value hptow other=[hptow()];
    run;

data my_cars;
    set sashelp.cars; 
    format horsepower hptow.;
run;

But results look something like that:

197610.
201339W

How do i change this mistake and have needed output format for formated column?

Upvotes: 1

Views: 251

Answers (1)

Tom
Tom

Reputation: 51566

Set a LENGTH for the variable you are calculating in the function. If you want the space in front of the W then don't remove it by using the CATS() function.

length n $14;
n = put(c*745.7,commax8.2) || ' Watts' ;

Set a default width for the format.

value hptow (default=14) other=[hptow()];

But why not just use a PICTURE format?

proc format ;
picture hptow 
 low-high = '00009,99 Watt' (mult=74570) 
;
run;

Results:

410   data test;
411     do hp=1,2,5,12 ;
412       w=hp*745.7 ;
413       put hp= w= @20 hp hptow.;
414     end;
415   run;

hp=1 w=745.7         745,70 Watt
hp=2 w=1491.4       1491,40 Watt
hp=5 w=3728.5       3728,50 Watt
hp=12 w=8948.4      8948,40 Watt

Upvotes: 3

Related Questions