Is there a way to create a custom format that changes depending on the length specified when the format is applied? For example, how WORDDATE changes format depending on the length given (3, 9, 12, and 20).
Writes date values as the name of the month, the day, and the year in the form month-name dd, yyyy.
Yes. What logic do you want in the format?
For a hypothetical example take fiscal year FMTNAME="FY". Define a format such that the format would help produce these results:
SAS Statement
|
Result
|
---|---|
put today Fy6.; |
FY1920 |
put today Fy7.;
|
FY19-20
|
put today Fy9.;
|
FY2019-20
|
put today Fy11.; |
FY2019-2020
|
Hi @PhilC ,
I know it is not the answer to your question but may FCMP will be sufficient?
options cmplib = _null_;
proc fcmp outlib = work.f.p;
function FY(x,l) $ 11;
length val $ 11;
select;
when (1<= l <= 6) do; val = cats("FY",mod(year(x)-1,100),mod(year(x),100)); end;
when ( l < 9) do; val = cats("FY",mod(year(x)-1,100),"-",mod(year(x),100)); end;
when ( l < 11) do; val = cats("FY",year(x)-1,"-",mod(year(x),100)); end;
when ( l = 11) do; val = cats("FY",year(x)-1,"-",year(x)); end;
otherwise val="**";
end;
return (val);
endsub;
run;
options cmplib = work.f;
data test;
array a[12] $ 11;
do i=1 to 12;
a[i]=FY(today(),i);
end;
run;
All the best
Bart
You really need to be specific about how you expect to use it.
That is already built into custom formats with the options DEFAULT and MAX . Default sets the default display length, calculated from values if not supplied. You can specify any n up to the default. If you specify a value for MAX in the format then you can specify the display length n up to that value. Appearance in output will be highly dependent on procedure used as most will left justify output by default.
With custom formats to insert leading spaces you will likely need to use a Put option -R.
proc format library=work; value mynum ( max=30) 1 ='one'; run; data junk;; term=1; put term mynum. -r; put term mynum3.-r; put term mynum9.-r; put term mynum20.-r; run;
Other than report procedures you may not get leading space appearance without modifying table templates for ODS objects.
In report procedures such as Report or Tabulate you would need to use the Style override ASIS to get leading spaces.
@ballardw , thanks, but that example is just way too trivial and not at all helpful.
But the PUT -r option is something that I find that I could use, in another context, and that I didn't previously know about.
@PhilC wrote:
@ballardw , thanks, but that example is just way too trivial and not at all helpful.
But the PUT -r option is something that I find that I could use, in another context, and that I didn't previously know about.
Well, since you didn't specify how you want an example to work and picked on Date values, which SAS does some very interesting things not shown to us (such as not displaying years past 9999 in any way shape or form even though the date manipulation functions don't have issues until around year 20,000) couldn't come with anything other than trivial.
Examples of actual values and the changing appearance desired would be needed to get a more realistic example.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.