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 ;
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)
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
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)
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.