```Date: Fri, 19 Sep 1997 02:11:14 GMT Reply-To: Xlr82sas Sender: "SAS(r) Discussion" From: Xlr82sas Organization: AOL http://www.aol.com Subject: Storing and executing functions in formats symput/resolve Hi SAS-Lers, Based on the value of one variable we want to perform some arbitrary function of two other variables. The functions are stored in formats. This methodology can be extended. Especially in light of new SCL functions in Base SAS. Code is also on my website. This method may be very slow. %macro utlfmtx ( utitle = Example of functions stored in formats, uobj=utlfmtx, /*-------------------------------------*\ | All inputs are generated internally | \*-------------------------------------*/ /*-------------------------------------*\ | | | Process | | | | Statement of problem | | | | This is a variation of a post by | | Ian Whitlock about a year ago | | | | | | Based on the value of one variable | | we want to perform some arbitrary | | function of two other variables | | This methodology can be extended. | | Especially in light of new SCL | | functions in Base SAS. | | | | Exactly like this: | | | | function has the form | | | | if w = 1 then z =25 * x * y; | | if w = 2 then z = 7 * x * y; | | if w = 3 then z = 9 * x * y; | | if w = 4 then z =13 * x * y; | | if w = 5 then z = 4 * x * y; | | if w = 6 then z = 2 * x * 2*y; | | | | Given w create format that yeilds | | the linear function of x and y. | | | | Use the macro compiler to evaluate | | the function. | | | | | \*-------------------------------------*/ /*-------------------------------------*\ | Outputs | | | | Function evaluations are shown in | | SAS log log enclosed | | | \*-------------------------------------*/ ) / des = "Example of functions stored in formats"; /*-----------------------------------*\ | | | AUTHOR ROGER DEANGELIS 09AUG97 | | | | COMPUCRAFT INC. | | 49 SPACKENKILL RD | | POUGHKEEPSIE, NY 12603 | | | | OFC: 914-463-2770 | | FAX 914-462-7595 | | | | COMPUCRAFT IS NOT RESPONSIBLE FOR | | PROBLEMS ASSOCIATED WITH THIS CODE.| | | | WIN95 SAS612 | | | | USERS ARE FREE TO DO WHATEVER | | THEY WANT WITH THIS CODE. | | | | I PUT THIS CODE IN THE PUBLIC | | DOMAIN. | | | \*-----------------------------------*/ proc format; value function 1 = '25 * &x * &y ' 2 = ' 7 * &x * &y ' 3 = ' 9 * &x * &y ' 4 = '13 * &x * &y ' 5 = ' 4 * &x * &y ' 6 = ' 2 * &x * &y ' ; run; data utlfmtx1 ( label = "Functions in formats" ); do w = 1 to 6; x = 2*w; y = 3*w; call symput ( 'x', x ); call symput ( 'y', y ); z = resolve ( '%eval('!!put( w, function.)!!');' ); put z =; end; run; %mend utlfmtx; %utlfmtx; /* The log MLOGIC(UTLFMTX): Beginning execution. MLOGIC(UTLFMTX): Parameter UTITLE has value Example of functions stored in formats MLOGIC(UTLFMTX): Parameter UOBJ has value utlfmtx MPRINT(UTLFMTX): PROC FORMAT; MPRINT(UTLFMTX): VALUE FUNCTION 1 = '25 * &x * &y ' 2 = ' 7 * &x * &y ' 3 = ' 9 * &x * &y ' 4 = '13 * &x * &y ' 5 = ' 4 * &x * &y ' 6 = ' 2 * &x * &y ' ; WARNING: Format FUNCTION is already on the library. NOTE: Format FUNCTION has been output. MPRINT(UTLFMTX): RUN; NOTE: The PROCEDURE FORMAT used 0.17 seconds. MPRINT(UTLFMTX): DATA UTLFMTX1 ( LABEL = Functions in formats ); MPRINT(UTLFMTX): DO W = 1 TO 6; MPRINT(UTLFMTX): X = 2*W; MPRINT(UTLFMTX): Y = 3*W; MPRINT(UTLFMTX): CALL SYMPUT ( 'x', X ); MPRINT(UTLFMTX): CALL SYMPUT ( 'y', Y ); MPRINT(UTLFMTX): Z = RESOLVE ( '%eval('!!PUT( W, FUNCTION.)!!');' ); MPRINT(UTLFMTX): PUT Z =; MPRINT(UTLFMTX): END; MPRINT(UTLFMTX): RUN; NOTE: Numeric values have been converted to character values at the places given by: (Line):(Column). 806:200 806:233 SYMBOLGEN: Macro variable X resolves to 2 SYMBOLGEN: Macro variable Y resolves to 3 MPRINT(UTLFMTX): 150; Z=150; SYMBOLGEN: Macro variable X resolves to 4 SYMBOLGEN: Macro variable Y resolves to 6 MPRINT(UTLFMTX): 168; Z=168; SYMBOLGEN: Macro variable X resolves to 6 SYMBOLGEN: Macro variable Y resolves to 9 MPRINT(UTLFMTX): 486; Z=486; SYMBOLGEN: Macro variable X resolves to 8 SYMBOLGEN: Macro variable Y resolves to 12 MPRINT(UTLFMTX): 1248; Z=1248; SYMBOLGEN: Macro variable X resolves to 10 SYMBOLGEN: Macro variable Y resolves to 15 MPRINT(UTLFMTX): 600; Z=600; SYMBOLGEN: Macro variable X resolves to 12 SYMBOLGEN: Macro variable Y resolves to 18 MPRINT(UTLFMTX): 432; Z=432; NOTE: The data set WORK.UTLFMTX1 has 1 observations and 4 variables. NOTE: The DATA statement used 0.71 seconds. MLOGIC(UTLFMTX): Ending execution. */ Roger J DeAngelis CompuCraft Inc XLR82SAS@aol.com ( Accelerate to SAS ) http://members.aol.com/xlr82sas/utl.html ```

Back to: Top of message | Previous page | Main SAS-L page