Search code examples
sas

Transform into decimal preventing trimming 0s


I'm in a situation like the one reported here: https://communities.sas.com/t5/New-SAS-User/Transform-integer-into-decimal/m-p/918994#M41156

Suppose, to have the following data (all numeric, not character):

data have;
input Hosp1 Hosp2 ;
cards;
71515   42731
48041 5761
4329    481
85201  42731
85220   5119
4280  40211
2510   4280
39   22
;
run;     

What I would like is the following output:

enter image description here

If I try using this code (as from yabwon):

data want;
set have;
array H hosp1 hosp2;
do over H;
  H = H/(10**max(0,ceil(log10(H))-3)); 
 end;
 format hosp1 hosp2 best.;
 run;

The output is as follows:

enter image description here

In other words 4280 becomes 428 but instead I would like 428.0 The same for 2510. Is there a way to prevent it?

Thank you in advance


Solution

  • There is no difference between the number that 428 represents and the number that 428.0 represents.

    It trailing zeros after the decimal point are meaningful then you need to store the value as a character string. (Note no need to bother with using ARRAYs for just two variables.)

    data want;
      set have;
      length chosp1 chosp2 $8 ;
      chosp1 = put(hosp1,7.-L);
      chosp1 = substr(chosp1,1,3)||'.'||substr(chosp1,4);
      chosp2 = put(hosp2,7.-L);
      chosp2 = substr(chosp2,1,3)||'.'||substr(chosp2,4);
    run;