This is as much a reminder to myself as anything else because the %CHAR built in function has the unfortunate behaviour of stripping leading zeroes when converting a number to a character value. Sometimes I need to keep those leading zeroes, for example when handling elderly date fields.
Fortunately, there’s %EDITC.
This function returns a character result representing the numeric value edited according to the edit code.
The edit codes allow you to do pull all sorts of neat little tricks but, for my purposes, the most useful one is the X edit code which (obscurely) ensures a hexadecimal F sign for positive values. Handily, this means that leading blanks are converted into leading zeroes when converting from numeric to character.
Here’s an example:
**Free // Converting to character with leading zeros ctl-opt dftactgrp(*no) actgrp(*new) main(Main); dcl-proc Main; dcl-pi *n end-pi; dcl-s numYear packed(4) inz(2016); dcl-s numMonth packed(2) inz(9); dcl-s numDay packed(2) inz(1); dcl-s charDate char(8) inz; dcl-c edit 'X'; // This converts the numeric fields ino string '20160901' charDate = %editc(numYear: edit) + %editc(numMonth: edit) + %editc(numDay: edit); // And then I can convert the string into a date and display it dsply %date(charDate: *ISO); return; end-proc;
And if you need a reminder of what the other edit codes are, or what they do, you’re welcome