BookmarkSubscribeRSS Feed
PhilC
Rhodochrosite | Level 12

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).

WORDDATE Format

Writes date values as the name of the month, the day, and the year in the form month-name dd, yyyy.

The example table uses the input value of 19158, which is the SAS date value that corresponds to June 14, 2012.
SAS Statement
Result
put term worddate3.; 
Jun
 
put term worddate9.;
     June
put term worddate12.;
Jun 14, 2012
 
put term worddate20.; 
       June 14, 2012
7 REPLIES 7
PeterClemmensen
Tourmaline | Level 20

Yes. What logic do you want in the format?

PhilC
Rhodochrosite | Level 12

@PeterClemmensen :

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
yabwon
Onyx | Level 15

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

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



ballardw
Super User

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.

PhilC
Rhodochrosite | Level 12

@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. 

ballardw
Super User

@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.

PhilC
Rhodochrosite | Level 12
@ballardw , you're right, I need to re-word my question

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 7 replies
  • 910 views
  • 1 like
  • 4 in conversation