Advertisement
leannemcn13

dec_align macro

Mar 7th, 2018
1,654
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SAS 1.75 KB | None | 0 0
  1. /**********************************************************
  2. *
  3. * Program Name   : dec_align.sas
  4. * Level / Study  :
  5. * Type           : Macro
  6. * Description    : Fix aligment issues
  7. *  
  8. *
  9. * Author         : Leanne McNaught
  10. * Date Created   : 27OCT2017
  11. * Program Status : Draft
  12. *
  13. *
  14. **********************************************************
  15. * Amended        :
  16. * Date Amended   :
  17. * Amended By     :
  18. **********************************************************/
  19.  
  20. %macro dec_align(dsin=, test=, aval=, dsout=);
  21. proc sql noprint;
  22.   select type
  23.   into: type
  24.   from sashelp.vcolumn
  25.   where libname= "WORK" and memname= %upcase("&dsin") and name= %upcase("&aval");
  26. quit;
  27.  
  28.  
  29. data check;
  30.   set &dsin. %if &type = num %then %do;(rename=(&aval=test))%end;;
  31.  
  32.   n= _n_;
  33.   %if &type = num %then %do;
  34.     &aval= strip(put(test, best.));
  35.   %end;
  36. run;
  37.  
  38. data check1;
  39.   set check;
  40.  
  41.   len= length(&aval);
  42.   dec= find(&aval, ".");
  43.  
  44.   if dec ne 0 then postlen= length(substr(&aval, dec, len)) - 1;
  45.   else postlen= .;
  46.   if dec ne 0 then prelen= length(substr(&aval, 1, dec)) - 1;
  47.   else prelen= length(substr(&aval, 1, len));
  48. run;
  49.  
  50. proc sql noprint;
  51. select max(prelen)
  52. into: max
  53. from check1;
  54.  
  55. select count(*)
  56. into: count trimmed
  57. from check1;
  58. quit;
  59.  
  60. data check1a;
  61.   set check1;
  62.  
  63.   max= &max;
  64.   if postlen ne . then do;
  65.    maxlen= max + postlen + 1;
  66.    decput= strip(put(maxlen, best.)) || "." || strip(put(postlen, best.));
  67.  end;
  68. run;
  69.  
  70. data &dsout;
  71.   set check1a;
  72.   length &aval._dec $200;
  73.  
  74.  do i = 1 to &count;
  75.    if n= i then do;
  76.    if postlen= . then &aval._dec= put(input(&aval, best.), &max..);
  77.    else if postlen ne . then do;
  78.      &aval._dec= putn(input(&aval,best.), decput);
  79.      end;
  80.    end;
  81.  end;
  82.  
  83. run;
  84. %mend dec_align;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement