Macro to create an ordering (for PROC REPORT)


/ Published in: SAS
Save to your folder(s)



Copy this code and paste it in your HTML
  1. *** ORDER MACRO ***;
  2. * This macro creates a format that will order PROC REPORT across columns;
  3. * according to the order the values are input, see examples below;
  4. * &order is a pipe(|) delimited list of values to be ordered;
  5. * &fmtname is the name of the format statement to be output;
  6. %macro order_fmt(fmtname=,order=);
  7.  
  8. %local icol ncol nspace ispace space;
  9.  
  10. /* Start defining format */
  11. proc format;
  12. value &fmtname
  13.  
  14. /* Loop over the columns specified in &order */
  15. %let ncol = %sysfunc(countw(%bquote(&order),|));
  16. %do icol = 1 %to &ncol;
  17.  
  18. /* Prefix formatted values with spaces to enforce ordering*/
  19. %let nspace = %eval(&ncol + 1 - &icol);
  20. %let space = %str();
  21. %do ispace = 1 %to &nspace;
  22. %let space = &space%str( );
  23. %end;
  24.  
  25. /* Format line - TRIM assumes that input data does not have prefix spaces*/
  26. "%qleft(%qtrim(%qscan(&order,&icol,|)))" = "&space%qleft(%qtrim(%qscan(&order,&icol,|)))"
  27.  
  28. %end;
  29.  
  30. ;
  31. run;
  32.  
  33. %mend order_fmt;

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.