[{"kind":1,"language":"markdown","value":"# **Le petit SAS package** \r\n## a workshop about the \"*Hello World*\" SAS package.\r\n\r\nby [**Bartosz Jabłoński**](https://linkedin.com/in/yabwon)\r\n\r\n[LinkedIn](https://linkedin.com/in/yabwon)\r\n[GitHub](https://github.com/yabwon)\r\n\r\n---\r\n\r\n<!-- ![Cover](./le_petit_SAS_package.png) -->\r\n<img src=\"./le_petit_SAS_package.png\" alt=\"Le petit SAS package\" style=\"height:800px;\"/>\r\n\r\n\r\n\r\n---","outputs":[]},{"kind":1,"language":"markdown","value":"---\r\n\r\n## Links and locations\r\n\r\n[**SAS Packages Framework**](https://github.com/yabwon/SAS_PACKAGES) - this is the place where the SAS Packages Framework lives.\r\n\r\n\r\n[**Hands on Workshop Materials**](https://github.com/yabwon/HoW-SASPackages) - this is the location for tutorial materials that will teach you how to work with SAS packages and that will take you \"from 0 to hero\" in SAS packages world.\r\n\r\n[SPF's introductory video series](https://www.youtube.com/playlist?list=PLeMzGEImIT5eV13IGXQIgWmTFCJt_cLZG) - this is a YouTube introductory video series explaining the basics.\r\n\r\n\r\n[**SAS Packages Archive**](https://github.com/SASPAC) - some of publicly available SAS packages are located here.\r\n\r\n[PharmaForest](https://github.com/PharmaForest) - pharma industry dedicated SAS packages are there.\r\n\r\n---","outputs":[]},{"kind":2,"language":"sas","value":"options nofullstimer stimer nomprint nosymbolgen nomlogic; \r\ndata _null_;\r\n  put \"WARNING- Welcome at NJSUG meetup!\";\r\nrun;","outputs":[]},{"kind":1,"language":"markdown","value":"## autoexec.sas\r\n\r\nI'm keeping my session setup in the `autoexec.sas` file.","outputs":[]},{"kind":2,"language":"sas","value":"/* this is an optional step - I basically have my session configuration in autoexec */\r\n/*\r\n%include \"C:\\SAS_WORK\\autoexec.sas\";\r\n\r\n%put %workpath();\r\n\r\n%put &sysLoadedPackages.;\r\n*/","outputs":[]},{"kind":1,"language":"markdown","value":"But it can be replaced, for example, by something like this:","outputs":[]},{"kind":2,"language":"sas","value":"/* set \"current working path\" to WORK location */\r\n%put %sysfunc(DLGCDIR(%sysfunc(PATHNAME(work))));\r\nfilename _ \".\";\r\nfilename _ list;\r\nfilename _ clear;\r\n\r\n\r\n/* enable the SAS Packages Framework */\r\nfilename packages \"C:\\SAS_WORK\\SAS_PACKAGES\"; /* filename packages \"/sas/PACKAGES\"; */\r\n%include packages(SPFinit.sas);\r\n\r\n/* Load SAS packages */\r\n%loadPackageS(SQLinDS BasePlus)\r\n/* %loadPackageS(macroArray, DFA, GSM, bpUTiL, maxims4sas, evExpress) */\r\n\r\n\r\nresetline;\r\n/* List loaded packages */\r\n%put NOTE- SAS session with SAS Packages: &SYSLoadedPackages.;\r\n\r\n%put NOTE- %workpath(); /* basePlus macro */","outputs":[]},{"kind":1,"language":"markdown","value":"---\r\n\r\n# Code types\r\n\r\nSAS Packages can contain various code types, including: \r\n- macros, \r\n- FCMP functions, \r\n- formats & informats, \r\n- IML modules, \r\n- DS2 packages & threads, \r\n- CAS-L functions, \r\n- data sets, \r\n- libraries, etc.\r\n\r\nFull list is provided in the [Training Materials](https://github.com/yabwon/HoW-SASPackages).\r\n\r\nToday we will go with 3 basic: a macros, a function, and a format.","outputs":[]},{"kind":1,"language":"markdown","value":"## A Macro\r\n\r\nThis macro prints the fox's quotes to the log.","outputs":[]},{"kind":2,"language":"sas","value":"resetline;\r\n\r\n%macro fox(quote);\r\n  %local n e w;\r\n  %let n = NOTE;\r\n  %let e = ERROR;\r\n  %let w = WARNING;\r\n  %if 1=%superq(quote) %then \r\n    %do;\r\n      %put &n.- And now here is my secret, a very simple secret:; \r\n      %put &n.- It is only with the heart that one can see rightly%str(;);\r\n      %put &n.- what is essential is invisible to the eye.;\r\n    %end;\r\n  %else\r\n  %if 2=%superq(quote) %then \r\n    %do;\r\n      %put &w.- It is the time you have wasted for your rose;\r\n      %put &w.- that makes your rose so important.;\r\n      %put &w.- Men have forgotten this truth. But you must not forget it.; \r\n      %put &w.- You become responsible, forever, for what you have tamed.;\r\n      %put &w.- You are responsible for your rose...;\r\n    %end;\r\n  %else\r\n    %do;\r\n      %put &e.- One only understands the things that one tames.; \r\n      %put &e.- Men have no more time to understand anything.;\r\n      %put &e.- They buy things all ready made at the shops.; \r\n      %put &e.- But there is no shop anywhere where one can buy friendship,; \r\n      %put &e.- and so men have no friends any more. If you want a friend, tame me...;\r\n    %end;\r\n%mend fox;\r\n\r\n%fox(1)\r\n%fox(2)\r\n%fox()","outputs":[]},{"kind":1,"language":"markdown","value":"## A Format\r\n\r\nThis format displays values from 1 to 4 as rose's quotes.","outputs":[]},{"kind":2,"language":"sas","value":"resetline;\r\n\r\nPROC FORMAT;\r\n  value rose\r\n  1=\"Ah! I am scarcely awake. I beg that you will excuse me. My petals are still all disarranged...\"\r\n  2=\"Of course I love you. It is my fault that you have not known it all the while. [...] Try to be happy...\"\r\n  3=\"My cold is not so bad as all that... The cool night air will do me good. I am a flower.\"\r\n  4=\"Well, I must endure the presence of two or three caterpillars if I wish to become acquainted with the butterflies.\"\r\n  other=\"ERROR: QUOTE OUT OF RANGE!\"\r\n  ;\r\nRUN;\r\n\r\n\r\ndata _null_;\r\n  do i = 1 to 5;\r\n    put \"NOTE- \" i rose. /;\r\n  end;\r\nrun;","outputs":[]},{"kind":1,"language":"markdown","value":"## A Function\r\n\r\nThis FCMP function returns the prince's quote: \"*If you please--draw me a sheep!*\", and generates random rose's quote in the LOG.","outputs":[]},{"kind":2,"language":"sas","value":"resetline;\r\n\r\nPROC FCMP outlib=work.little.prince;\r\n  function prince() $ 42;\r\n    file log;\r\n\r\n    length i $ 256;\r\n    r=rand('integer',1,4);\r\n    i = put(r, rose.);\r\n    put @1 \"RANDOM NOTE:\" i /;\r\n\r\n    return(\"If you please--draw me a sheep!\");\r\n  endfunc;\r\nQUIT;\r\n\r\nproc options option=cmplib;\r\nrun;","outputs":[]},{"kind":1,"language":"markdown","value":"For an FCMP function to work the `CMPLIB` option has to be updated.","outputs":[]},{"kind":2,"language":"sas","value":"options append=(cmplib=work.little);\r\n\r\nproc options option=cmplib;\r\nrun;\r\n\r\ndata _null_;\r\n  do i = 1 to 5;\r\n    prince=prince();\r\n    rc=sleep(1,0.2);\r\n  end;\r\n  put prince=;\r\nrun;","outputs":[]},{"kind":1,"language":"markdown","value":"---\r\n\r\n# **##############################**\r\n\r\n# *If you please - build me a SAS package!*\r\n\r\n# **##############################**\r\n\r\n---","outputs":[]},{"kind":1,"language":"markdown","value":"## The Directory\r\n\r\nIn the firs step, a directory for the package source has to be created.","outputs":[]},{"kind":2,"language":"sas","value":"options dlcreatedir;\r\n%let dir = R:\\NJSUG\\LePetitSASpackageDirectory;\r\n\r\nlibname p \"&dir.\";\r\n\r\nlibname p list;","outputs":[]},{"kind":1,"language":"markdown","value":"## The Description\r\n\r\nThis is the **description** file for the package.\r\n\r\nIt should be named `description.sas` and located in the package source directory. \r\n\r\nThe colon (`:`) is a field separator and is restricted in lines of the header part.\r\n\r\nThe part between `DESCRIPTION START:` and `DESCRIPTION END:` is a \"free format\" text part where the developer provides package description, additional notes, information, etc.","outputs":[]},{"kind":2,"language":"sas","value":"/* **HEADER** */\r\nType: Package\r\nPackage: LePetitSASpackage\r\nTitle: Le petit SAS package - a workshop about the \"*Hello World*\" SAS package.\r\nVersion: 0.0.1\r\nAuthor: Bartosz Jablonski\r\nMaintainer: Bartosz Jablonski (yabwon@gmail.com)\r\nLicense: MIT\r\nEncoding: UTF8\r\n\r\nRequired: \"Base SAS Software\"\r\nReqPackages: \"SQLinDS(2.3.3)\"\r\n\r\n\r\n/* **DESCRIPTION** */\r\n/* All the text below will be used for help notes */\r\nDESCRIPTION START:\r\n\r\nThe **LePetitSASpackage** package is an implementation \r\nof a little \"*Hello World*\" SAS package presented during \r\n**NJSUG** meetup.\r\n\r\nIt is build for fun, but also (or foremost) to show us \r\nhow easy it is to build SAS packages.\r\n\r\n*\"I have serious reason to believe that the planet from which \r\nthe little package came is the asteroid known as B612.\r\nThis asteroid has only once been seen through the telescope. \r\nThat was by a Turkish astronomer, in 1909.\"*\r\n\r\n---\r\n\r\nDESCRIPTION END:","outputs":[]},{"kind":1,"language":"markdown","value":"[**NOTE**] The `SQLinDS` package is added as a dependency just to show the it can be done. In normal circumstances, when a package doesn't have dependencies the `ReqPackage:` tag is skipped. The `Required:` tag is also just for demonstration.","outputs":[]},{"kind":1,"language":"markdown","value":"## The Directory, cont.","outputs":[]},{"kind":1,"language":"markdown","value":"### Ordering\r\n\r\nCode files have to be placed in proper type-directories. And ordered accordingly. In this case teh following setup will work.","outputs":[]},{"kind":2,"language":"sas","value":"/* 01_macro -> fox.sas */\r\n\r\n/* 02_formats -> rose.sas */\r\n\r\n/* 03_functions -> prince.sas */","outputs":[]},{"kind":1,"language":"markdown","value":"Directories structure can be easily created by ourselves with the `dlcreatedir` option. If the option is not available, then it can be done by hand.","outputs":[]},{"kind":2,"language":"sas","value":"resetline;\r\noptions dlcreatedir;\r\n\r\nlibname p \"&dir.\\01_macro\";\r\nlibname p \"&dir.\\02_formats\";\r\nlibname p \"&dir.\\03_functions\";\r\n\r\nlibname p clear;","outputs":[]},{"kind":1,"language":"markdown","value":"## Code Preparation","outputs":[]},{"kind":1,"language":"markdown","value":"### Help Notes","outputs":[]},{"kind":2,"language":"sas","value":"/*** HELP START ***//*\r\n\r\nThis is a little help note.\r\n\r\n*//*** HELP END ***/","outputs":[]},{"kind":2,"language":"sas","value":"/*** HELP START ***//*\r\n\r\nThe `%fox()` macro prints what does the fox says...\r\n\r\n---\r\n\r\n### Syntax: ############################### \r\n\r\n~~~~~~~~~~sas\r\n%fox(<quote>)\r\n~~~~~~~~~~\r\n\r\n### Arguments: ############################\r\n\r\n- `quote` - The number of fox's quote.\r\n            When missing or incorrect, \r\n            the default quote is displayed.\r\n\r\n\r\n### Example: ##############################\r\n\r\nPrint quote number 1:\r\n~~~~~~~~~~sas\r\n%fox(1)\r\n~~~~~~~~~~\r\n\r\n---\r\n\r\n*//*** HELP END ***/","outputs":[]},{"kind":2,"language":"sas","value":"/*** HELP START ***//*\r\n\r\nThe `rose.` format prints what does the rose says...\r\n\r\n---\r\n\r\n### Example: ##############################\r\n\r\nPrint quote number 2:\r\n~~~~~~~~~~sas\r\ndata _null_;\r\n  r=2;\r\n  put r rose.;\r\nrun;\r\n~~~~~~~~~~\r\n\r\n---\r\n\r\n*//*** HELP END ***/","outputs":[]},{"kind":2,"language":"sas","value":"/*** HELP START ***//*\r\n\r\nThe `prince()` function asks to draw a sheep...\r\n\r\n---\r\n\r\n### Arguments: ############################\r\n\r\nThe function has no arguments.\r\n\r\n### Dependencies: #########################\r\n\r\nThe `prince()` function requires \r\nthe `rose.` format to work.\r\n\r\n### Example: ##############################\r\n\r\nAsk for a sheep:\r\n~~~~~~~~~~sas\r\ndata _null_;\r\n  s=prince();\r\n  put s=;\r\nrun;\r\n~~~~~~~~~~\r\n\r\n---\r\n\r\n*//*** HELP END ***/","outputs":[]},{"kind":1,"language":"markdown","value":"### One file - one object","outputs":[]},{"kind":2,"language":"sas","value":"resetline;\r\n\r\n/* 01_macro -> fox.sas */\r\nfilename f \"&dir.\\01_macro\\fox.sas\";\r\ndata _null_;\r\n  file f;\r\n  infile CARDS4;\r\n  input;\r\n  put _infile_;\r\nCARDS4;\r\n/*** HELP START ***//*\r\n\r\nThe `%fox()` macro prints what does the fox says...\r\n\r\n---\r\n\r\n### Syntax: ############################### \r\n\r\n~~~~~~~~~~sas\r\n%fox(<quote>)\r\n~~~~~~~~~~\r\n\r\n### Arguments: ############################\r\n\r\n- `quote` - The number of fox's quote.\r\n            When missing or incorrect, \r\n            the default quote is displayed.\r\n\r\n\r\n### Example: ##############################\r\n\r\nPrint quote number 1:\r\n~~~~~~~~~~sas\r\n%fox(1)\r\n~~~~~~~~~~\r\n\r\n---\r\n\r\n*//*** HELP END ***/\r\n\r\n%macro fox(quote);\r\n  %local n e w;\r\n  %let n = NOTE;\r\n  %let e = ERROR;\r\n  %let w = WARNING;\r\n  %if 1=%superq(quote) %then \r\n    %do;\r\n      %put &n.- And now here is my secret, a very simple secret:; \r\n      %put &n.- It is only with the heart that one can see rightly%str(;);\r\n      %put &n.- what is essential is invisible to the eye.;\r\n    %end;\r\n  %else\r\n  %if 2=%superq(quote) %then \r\n    %do;\r\n      %put &w.- It is the time you have wasted for your rose;\r\n      %put &w.- that makes your rose so important.;\r\n      %put &w.- Men have forgotten this truth. But you must not forget it.; \r\n      %put &w.- You become responsible, forever, for what you have tamed.;\r\n      %put &w.- You are responsible for your rose...;\r\n    %end;\r\n  %else\r\n    %do;\r\n      %put &e.- One only understands the things that one tames.; \r\n      %put &e.- Men have no more time to understand anything.;\r\n      %put &e.- They buy things all ready made at the shops.; \r\n      %put &e.- But there is no shop anywhere where one can buy friendship,; \r\n      %put &e.- and so men have no friends any more. If you want a friend, tame me...;\r\n    %end;\r\n%mend fox;\r\n;;;;\r\nrun;","outputs":[]},{"kind":2,"language":"sas","value":"/* 02_formats -> rose.sas */\r\nfilename f \"&dir.\\02_formats\\rose.sas\";\r\ndata _null_;\r\n  file f;\r\n  infile CARDS4;\r\n  input;\r\n  put _infile_;\r\nCARDS4;\r\n/*** HELP START ***//*\r\n\r\nThe `rose.` format prints what does the rose says...\r\n\r\n---\r\n\r\n### Example: ##############################\r\n\r\nPrint quote number 2:\r\n~~~~~~~~~~sas\r\ndata _null_;\r\n  r=2;\r\n  put r rose.;\r\nrun;\r\n~~~~~~~~~~\r\n\r\n---\r\n\r\n*//*** HELP END ***/\r\n\r\nvalue rose\r\n1=\"Ah! I am scarcely awake. I beg that you will excuse me. My petals are still all disarranged...\"\r\n2=\"Of course I love you. It is my fault that you have not known it all the while. [...] Try to be happy...\"\r\n3=\"My cold is not so bad as all that... The cool night air will do me good. I am a flower.\"\r\n4=\"Well, I must endure the presence of two or three caterpillars if I wish to become acquainted with the butterflies.\"\r\nother=\"ERROR: QUOTE OUT OF RANGE!\"\r\n;\r\n;;;;\r\nrun;","outputs":[]},{"kind":2,"language":"sas","value":"/* 03_functions -> prince.sas */\r\nfilename f \"&dir.\\03_functions\\prince.sas\";\r\ndata _null_;\r\n  file f;\r\n  infile CARDS4;\r\n  input;\r\n  put _infile_;\r\nCARDS4;\r\n/*** HELP START ***//*\r\n\r\nThe `prince()` function asks to draw a sheep...\r\n\r\n---\r\n\r\n### Arguments: ############################\r\n\r\nThe function has no arguments.\r\n\r\n### Dependencies: #########################\r\n\r\nThe `prince()` function requires \r\nthe `rose.` format to work.\r\n\r\n### Example: ##############################\r\n\r\nAsk for a sheep:\r\n~~~~~~~~~~sas\r\ndata _null_;\r\n  s=prince();\r\n  put s=;\r\nrun;\r\n~~~~~~~~~~\r\n\r\n---\r\n\r\n*//*** HELP END ***/\r\n\r\nfunction prince() $ 42;\r\n  file log;\r\n\r\n  length i $ 256;\r\n  r=rand('integer',1,4);\r\n  i = put(r, rose.);\r\n  put @1 \"RANDOM NOTE:\" i /;\r\n\r\n  return(\"If you please--draw me a sheep!\");\r\nendfunc;\r\n;;;;\r\nrun;","outputs":[]},{"kind":1,"language":"markdown","value":"### Tests\r\n\r\nTests are optionally available, i.e., they are available if the `XCMD` option is on. \r\n\r\nOne test of loading a package is always automatically executed. All other tests are developer's job to do.\r\n\r\nThe SPF automatically points to the package location for tests, developer doesn't have to worry.","outputs":[]},{"kind":2,"language":"sas","value":"resetline;\r\noptions dlcreatedir;\r\n\r\nlibname p \"&dir.\\99_test\";\r\nlibname p clear;","outputs":[]},{"kind":2,"language":"sas","value":"/* 99_test -> test_success.sas */\r\nfilename f \"&dir.\\99_test\\test_success.sas\";\r\ndata _null_;\r\n  file f;\r\n  infile CARDS4;\r\n  input;\r\n  put _infile_;\r\nCARDS4;\r\n%put testing macro:;\r\n%fox(1)\r\n%fox(2)\r\n%fox()\r\n\r\ndata _null_;\r\n  put \"Testing format:\";\r\n  do i = 1 to 4;\r\n    put \"NOTE- \" i rose. /;\r\n  end;\r\nrun;\r\n\r\ndata _null_;\r\n  put \"Testing function:\";\r\n  do i = 1 to 5;\r\n    prince=prince();\r\n    rc=sleep(1,0.2);\r\n  end;\r\n  put;\r\nrun;\r\n;;;;\r\nrun;","outputs":[]},{"kind":2,"language":"sas","value":"/* 99_test -> test_fail_e1w0.sas */\r\nfilename f \"&dir.\\99_test\\test_fail_e1w0.sas\";\r\ndata _null_;\r\n  file f;\r\n  infile CARDS4;\r\n  input;\r\n  put _infile_;\r\nCARDS4;\r\ndata _null_;\r\n  put \"Testing format (should print error):\";\r\n  do i = 5;\r\n    put i rose. /;\r\n  end;\r\nrun;\r\n;;;;\r\nrun;","outputs":[]},{"kind":1,"language":"markdown","value":"## Generate Package\r\n\r\nIn this session we already have the SPF enabled, but in the development process you have to point the location for packages and enable the framework.","outputs":[]},{"kind":2,"language":"sas","value":"filename packages \"C:\\SAS_WORK\\SAS_PACKAGES\";\r\n%include packages(SPFinit.sas);","outputs":[]},{"kind":1,"language":"markdown","value":"Options can be easily reminded (in he LOG) by calling the macro with the `HELP` keyword.","outputs":[]},{"kind":2,"language":"sas","value":"%generatePackage(HELP)","outputs":[]},{"kind":2,"language":"sas","value":"resetline;\r\n\r\n/* Generate Package */\r\n\r\n%generatePackage(\r\n R:\\NJSUG\\LePetitSASpackageDirectory\r\n,markdownDoc=1\r\n,packages=C:\\SAS_WORK\\SAS_PACKAGES\r\n)\r\n\r\n/* REMEMBER! Always check the log.*/","outputs":[]},{"kind":1,"language":"markdown","value":"# --The End--\r\n\r\nThe package is ready! It can be shared with other SAS programmers now.\r\n\r\n---","outputs":[]},{"kind":1,"language":"markdown","value":"#\r\n\r\n##\r\n\r\n###\r\n\r\n---\r\n\r\n# Start New SAS Session\r\n\r\nIn a brand new SAS session try out how the created package works.","outputs":[]},{"kind":2,"language":"sas","value":"/* create directory for SAS packages */\r\n\r\nresetline;\r\noptions dlcreatedir;\r\nlibname p \"R:\\NJSUG\\trySASpackages\";\r\nlibname p clear;","outputs":[]},{"kind":2,"language":"sas","value":"/* install the SAS Packages Framework and the SQLinDS package */\r\n\r\nfilename packages \"R:\\NJSUG\\trySASpackages\";\r\n\r\nfilename SPFinit url \"https://bit.ly/SPFinit\";\r\n%include SPFinit; /* enable the framework */\r\nfilename SPFinit clear;\r\n\r\n%installPackage(SPFinit SQLinDS)","outputs":[]},{"kind":1,"language":"markdown","value":"Copy, manually for now, the LePetitSASpackage (the zip file) to packages directory.","outputs":[]},{"kind":2,"language":"sas","value":"/* enable the SPF and load the LePetitSASpackage */\r\n\r\nfilename packages \"R:\\NJSUG\\trySASpackages\";\r\n%include packages(SPFinit.sas);\r\n%listPackages()\r\n\r\n\r\n%loadPackage(LePetitSASpackage)\r\n\r\n/* try it */\r\n%fox(1)\r\n%fox(2)\r\n\r\n%put %sysfunc(prince());\r\n\r\ndata _null_;\r\n  p = prince();\r\n  put p=;\r\nrun;","outputs":[]},{"kind":1,"language":"markdown","value":"---","outputs":[]},{"kind":1,"language":"markdown","value":"---","outputs":[]}]