1 / 32

Proc FCMP – Data Step Functions

Proc FCMP – Data Step Functions. Denise.Poll@sas.com. What is Proc FCMP?. “SAS Function Compiler” Build functions using DATA step syntax Store the functions in a data set Call the functions from the DATA step just as you would any other SAS function

otylia
Download Presentation

Proc FCMP – Data Step Functions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Proc FCMP – Data Step Functions Denise.Poll@sas.com

  2. What is Proc FCMP? • “SAS Function Compiler” • Build functions using DATA step syntax • Store the functions in a data set • Call the functions from the DATA step just as you would any other SAS function • Lots more capabilities, just not covered today

  3. Proc FCMP - History • Prior to Version 9.2 – Limited to Product Procs • SAS/STAT • SAS/ETS • SAS/OR • Post 9.2 available – Data step syntax

  4. Advantages of Writing Your Own Functions • Function makes a program easier to read, write and modify • Reuse and quality control • Test once use many times • Program/function independence

  5. Syntax to Create a Function - 3 level name Proc FCMP outlib = << Ids where to store sasuser.MySubs.MathFncs; << DS and package function day_date( indate, type $); if type = "DAYS" then wkday = weekday(indate); if type = "YEARS" then wkday = weekday(indate*365); return(wkday); endsub; run;

  6. Outlib – 3 Level Name - Package Proc FCMP outlib = sasuser.MySubs.MathFncs; • Library • Dataset • Package – collection of related functions • Could be by organized by application or by corporate department

  7. Pieces and Parts • 4 parts between FUNCTION statement ENDSUB keyword • Function name • day_date • One or more parameters • ( indate, type $); • A body of code • A RETURN statement • return(wkday);

  8. Syntax to Create a Function – Statement & Name Proc FCMP outlib = sasuser.MySubs.MathFncs; functionday_date( indate, type $); << stmt & name if type = "DAYS" then wkday = weekday(indate); if type = "YEARS" then wkday = weekday(indate*365); return(wkday); endsub; run;

  9. Syntax to Create a Function – Naming functionday_date( indate, type $); << stmt & name • Cannot have a function name that collides with a built-in SAS function name. An error message is generated if an attempt is made. • Name must be unique in the package • 2 packages with the same name – qualify name when calling • MathFncs.day_date

  10. Syntax to Create a Function - Arguments function day_date( indate, type $); << arguments function coffee_deal() $ ; << no argument Can have one or more arguments for a function. Specify character arguments by placing a dollar sign ($) after the argument name. In the above example: indateisnumeric and type is a character argument

  11. Syntax to Create a Function – Logic The DAY_DATE function converts a date to a numeric day of the week if type = "DAYS" then wkday = weekday(indate); if type = "YEARS" then wkday = weekday(indate*365);

  12. Usage of SAS Function - weekday Proc FCMP outlib = sasuser.MySubs.MathFncs; function day_date( indate, type $); if type = "DAYS" then wkday = weekday(indate); if type = "YEARS" then wkday = weekday(indate*365); return(wkday); endsub; run;

  13. Syntax to Create a Function – Return Proc FCMP outlib = sasuser.MySubs.MathFncs; function day_date( indate, type $); if type = "DAYS" then wkday = weekday(indate); if type = "YEARS" then wkday = weekday(indate*365); return(wkday); <<Returns a numeric value endsub; run;

  14. Calling the Function Options cmplib = sasuser.mysubs; Data work.usefunc; Numdays = day_date(‘13sep2011’d,”DAYS”); Put numdays=; run; Numdays = 3

  15. Options CMPLIB = Options cmplib = sasuser.mysubs; CMPLIB is a SAS option that supports usage with INSERT and APPEND Options insert=(cmplib=sasuser.meFirst) ; Options append=(cmplib=sasuser.meLast);

  16. Nesting and Scope

  17. Nesting and Scope DATA Step funA funB

  18. Scope – Independent “x” Variable function funB(value); x = value * 100; put 'In funB:' x=; return (x); endsub; function funA(value); x = value; put 'In funA:' x=; y = funB(x); return (y*10); endsub; data _null_; x = funA(5); put 'In DATA Step: ' x=; run;

  19. Scope – Usage data _null_; x = funA(5); put 'In DATA Step:‘ x=; run; Output to the SAS Log: • In funA: x=5 • In funB: x=500 • In DATA Step: x=5000

  20. Function Management • Protecting Functions • Listing the Source • Adding a Function • Removing a Function • Replacing a Function

  21. Protecting Functions • Operating System File Permissions • Unix: chmod • Windows: attrib • z/OS: RACF • libname perm "\\shared\perm" access=readonly; • SAS/Share Read-Only Library

  22. Listing the Function Source proc fcmp outlib=sasuser.funcs.trial; listfunc study_day;

  23. Adding a Function proc fcmp outlib=sasuser.funcs.trial; function study_day(start, event); n = event – start; if n >= 0 then n = n + 1; return(n); endsub;

  24. Removing a Function proc fcmp outlib=sasuser.funcs.trial; deletefunc study_day;

  25. Replacing a Function proc fcmp outlib=sasuser.funcs.trial; deletefunc study_day; function study_day(start, event); ...

  26. Replacing a Function

  27. Proc FCMP - Data Step Statements Not Supported • Data • Set • Merge • Update • Modify • Input • Infile (thru functions OPEN-FETCH, …)

  28. Proc FCMP – Power of Put • Use Put for debugging Results of a Put statement can go to the PRINT (default) and LOG destinations … FILE log; Put variable ;

  29. Acknowledgements and Questions • Thanks to GASUG for the invitation to present! • Thanks to Jason Secosky who provided a subset of the material for this presentation

More Related