BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
lopezr
Obsidian | Level 7

I'm fairly new to PROC FCMP and am wondering if it possible to provide a format name as an argument in a function defined using PROC FCMP. For example, in the below function to store values as `estimate (LCL, UCL)` I would like to allow the user to specify a format name of choice without having the restriction to only accepting a user defined format named __ESTF. Ideally I would have a function XparenthesisYZ(__x, __y, __z, __fmt) where __fmt is a format name. 

 

I use SAS 9.4M5 in a Linux environment and have tried various things without any luck. Appreciate any guidance.
Thanks!

 

 

**Define and store function ;
libname test "C:\FCMP_Test" ;

proc fcmp outlib=test.MyFunctions.Reporting ;
    **x (y, z) ;
    function XparenthesisYZ(__x, __y, __z) $ ;
    length __b $50 ;
    __b=compress(put(__x, __estf.))||" ("||
              compress(put(__y, __estf.))||", "||
              compress(put(__z, __estf.))||")" ;
    return (__b) ;
    endsub ;
run ;

**Define the format I want to use. This must be called __ESTF ;
proc format ;
    picture __Estf (round) 
        low   -  -1.1  ="00000000000009.9" (prefix = '-') 
        -1.1 <-<  0    = "9.99" (prefix = '-') 
        0     -<  1.1  ="9.99" 
        1.1   -   high = "00000000000009.9" ;
run ;

**Apply function to test data ;
options cmplib=test.MyFunctions ;

data test ;
    input OR OR_LCL OR_UCL ;
    OR_CI = XparenthesisYZ(or, or_lcl, or_ucl) ;
    datalines ;
2.3 1.3 3.3
0.856 0.555 2.55
0.057 0.864 3.444
     ;
run ;

title "Output" ;
proc print data=test noobs ;
run ;
title ;

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

To pass a format to a function pass it as a character value. Then use it with the PUTN() or PUTC() function.

proc fcmp outlib=WORK.MyFunctions.Reporting ;
  function XparenthesisYZ(est,ll,ul,fmt $) $ ;
    length result $50 ;
    result=catx(' ('
          ,putn(est,fmt)
          ,catx(', ',putn(ll,fmt),putn(ul,fmt))
          )||')'
    ;
    return (result) ;
  endsub ;
run ;

So let's test it:

data test ;
    input OR OR_LCL OR_UCL ;
    OR_CI = XparenthesisYZ(or, or_lcl, or_ucl,'__estf.') ;
    OR_CI2 = XparenthesisYZ(or, or_lcl, or_ucl,'z5.2') ;

datalines ;
2.3 -1.3 3.3
0.856 0.555 2.55
0.057 0.864 3.444
;

Results:

  OR     OR_LCL    OR_UCL         OR_CI                 OR_CI2

2.300    -1.300     3.300    2.3 (-1.3, 3.3)     02.30 (-1.30, 03.30)
0.856     0.555     2.550    0.86 (0.56, 2.6)    00.86 (00.56, 02.55)
0.057     0.864     3.444    0.06 (0.86, 3.4)    00.06 (00.86, 03.44)

View solution in original post

3 REPLIES 3
yabwon
Amethyst | Level 16

option 1) replace order, first make format then function:

**Define and store function ;
options dlcreatedir;
libname test "%sysfunc(pathname(work))/test" ;

**Define the format I want to use. This must be called __ESTF ;
proc format ;
    picture __Estf (round) 
        low   -  -1.1  ="00000000000009.9" (prefix = '-') 
        -1.1 <-<  0    = "9.99" (prefix = '-') 
        0     -<  1.1  ="9.99" 
        1.1   -   high = "00000000000009.9" ;
run ;

proc fcmp outlib=test.MyFunctions.Reporting ;
    **x (y, z) ;
    function XparenthesisYZ(__x, __y, __z) $ ;
    length __b $50 ;
    __b=compress(put(__x, __estf.))||" ("||
              compress(put(__y, __estf.))||", "||
              compress(put(__z, __estf.))||")" ;
    return (__b) ;
    endsub ;
run ;

**Apply function to test data ;
options cmplib=test.MyFunctions ;

data test ;
    input OR OR_LCL OR_UCL ;
    OR_CI = XparenthesisYZ(or, or_lcl, or_ucl) ;
    datalines ;
2.3 1.3 3.3
0.856 0.555 2.55
0.057 0.864 3.444
     ;
run ;

title "Output" ;
proc print data=test noobs ;
run ;
title ;

option 2) try function putn() which accepts format name as a string

 

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



Tom
Super User Tom
Super User

To pass a format to a function pass it as a character value. Then use it with the PUTN() or PUTC() function.

proc fcmp outlib=WORK.MyFunctions.Reporting ;
  function XparenthesisYZ(est,ll,ul,fmt $) $ ;
    length result $50 ;
    result=catx(' ('
          ,putn(est,fmt)
          ,catx(', ',putn(ll,fmt),putn(ul,fmt))
          )||')'
    ;
    return (result) ;
  endsub ;
run ;

So let's test it:

data test ;
    input OR OR_LCL OR_UCL ;
    OR_CI = XparenthesisYZ(or, or_lcl, or_ucl,'__estf.') ;
    OR_CI2 = XparenthesisYZ(or, or_lcl, or_ucl,'z5.2') ;

datalines ;
2.3 -1.3 3.3
0.856 0.555 2.55
0.057 0.864 3.444
;

Results:

  OR     OR_LCL    OR_UCL         OR_CI                 OR_CI2

2.300    -1.300     3.300    2.3 (-1.3, 3.3)     02.30 (-1.30, 03.30)
0.856     0.555     2.550    0.86 (0.56, 2.6)    00.86 (00.56, 02.55)
0.057     0.864     3.444    0.06 (0.86, 3.4)    00.06 (00.86, 03.44)
lopezr
Obsidian | Level 7
Thank you @Tom ! This is exactly what I wanted.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 3 replies
  • 1258 views
  • 2 likes
  • 3 in conversation