How to run SAS programs in Jupyter Notebook

41

We've just celebrated Earth Day, but I'm here to talk about Jupyter -- and the SAS open source project that opens the door for more learning. With this new project on the github.com/sassoftware page, SAS contributes new support for running SAS from within Jupyter Notebooks -- a popular browser-based environment used by professors and data scientists.

My colleague Amy Peters announced this during a SAS Tech Talk show at SAS Global Forum 2016. If you want to learn more about Jupyter and see the SAS support in action, then you can watch the video here.

Visit the project on GitHub: sas_kernel by sassoftware

Within Jupyter, the sas_kernel provides multiple ways to access SAS programming methods. The most natural method is to create a new SAS notebook, available from the New menu in the Jupyter Home window and from the File menu in an active notebook:

newsasnotebook
From a SAS notebook, you can enter and run SAS code directly from a cell:

sasnotebook
There is even a Notebook extension (./nbextensions/showSASLog) that can show you the SAS log.

The second way that you can run SAS code is by using special Jupyter "magics" supported by the sas_kernel. These magic commands look almost just like SAS macro calls (imagine that!). From within a Python language notebook, you can inject your SAS program code and pull in SAS results. This allows you to move easily between Python and SAS in a single environment. Here's a simple example:

%%SAS
proc means data=sashelp.cars;
run;
ods graphics / height=500 width=800;
proc sgplot data=sashelp.cars;
histogram msrp;
run;

How to get started


Update 25July2016: Support for Jupyter is now part of the SAS University Edition! See the SAS University Edition documentation for information about how to use it.


In SAS environments outside of SAS University Edition, here's what you need to run SAS with Jupyter:

  • SAS 9.4 or later running on Windows, Linux, or even z/OS (see support for SASPy, the underlying package)
  • Python 3 installed on the same machine (that's basically part of Linux)
  • Access to the OS shell to install/configure the Jupyter Notebook infrastructure and the sas_kernel.

End users of Jupyter Notebook do not need special privileges. And you can access it from a browser on any system: Windows, Mac, Linux...whatever. In the SAS Tech Talk video with Amy, we were running on my Windows laptop using Chrome, connecting to a Linux instance of Jupyter and SAS. The GitHub project has all of the doc and step-by-step instructions for installation.

What's next for SAS and Jupyter?

This is just the start for SAS in the Jupyter world. Amy says that she has already received lots of interest and feedback, and SAS is working to make the Jupyter Notebook approach available in something like SAS University Edition and SAS OnDemand for Academics. Stay tuned!

Share

About Author

Chris Hemedinger

Senior Manager, SAS Online Communities

+Chris Hemedinger is the manager of SAS Online Communities. Since 1993, Chris has worked for SAS as an author, a software developer, an R&D manager and a consultant. Inexplicably, Chris is still coasting on the limited fame he earned as an author of SAS For Dummies.  He also hosts the SAS Tech Talk webcasts each year from SAS Global Forum, connecting viewers with smart people from SAS R&D and the impressive work that they do.

41 Comments

  1. I wonder if there will be any possibility (some sort of add-inns or upgrade) in a way that the same job can be done by SAS 9.3.

    • Chris Hemedinger
      Chris Hemedinger on

      I think we heard this request several times at SAS Global Forum. I know that Windows support is under consideration; I don't know the timetable.

    • I want to second the motion to make this work on windows. On a lark I googled "sas kernal jupyter notebook" and found this recent announcement. I am very excited. I use python + jupyter to memorialize my work ("ask the data questions and write down the answers") and have been wishing for the same thing with SAS.

      • Chris Hemedinger
        Chris Hemedinger on

        Hi Will, nothing to share yet. I did have a conversation with the developers last week, and they were looking into an approach...but it's premature to say when it will happen.

  2. The Linux requirement is a bit odd, since the Jupyter kernel could control SAS on Windows via the .NET interface using pywin32 or a similar module. I'll have to look at the source to see how the kernel connects to SAS.

    • Chris Hemedinger
      Chris Hemedinger on

      I think the team worked on Linux first, as that's a popular multi-user SAS platform where Jupyter can have a positive impact on teams. And also the SAS University Edition is delivered with a packaged Linux VM, so this will be a natural fit...eventually. I think Windows support is inevitable, just wasn't the focus right out of the gate. I suspect the mechanism will need to be a little different for Windows.

    • Chris Hemedinger
      Chris Hemedinger on

      Hi Bjorn, I'd guess the answer is Yes, on a technical level. However, SDD has some pretty strict auditing/authentication/process requirements, so I'm not sure if it's fit for the purpose. If you're working with a SAS-hosted SDD environment, it might be worth asking your SAS rep about this possibility.

  3. Pingback: Choose your own adventure with SAS Viya - SAS Users

  4. Pingback: Reproducible research: Is my SAS code enough? - A Shot in the Arm

  5. How does this work if I have SAS grid and Jupyter is on remote server? In short, if I have server based environments as opposed to SAS and Jupyter software on my pc?

    • Chris Hemedinger
      Chris Hemedinger on

      Hi Kevin, great question. I think that the way this works right now it doesn't play into the Grid environment. That is, you have Jupyter hosted on a single node where you have SAS available, and it launches/communicates with a single SAS process. I'll ping the developer (Jared) to see if he has anything to add about this.

    • Jared Dean

      Kevin,
      Currently the SAS Kernel can't take advantage of SAS Grid. Other organizations have groups connecting to grid nodes directly through SSH which allows a centralized Jupyter server that is located apart from SAS. You can see setup details from the inline comments here: https://github.com/sassoftware/saspy/blob/master/saspy/sascfg.py

      If SAS Grid and Jupyter interaction is a strong business need, I would encourage you to enter an issue at https://github.com/sassoftware/sas_kernel/issues

  6. Pingback: Using Jupyter and SAS together with SAS University Edition - The SAS Dummy

  7. Pingback: Reporting on GitHub accounts with SAS - The SAS Dummy

  8. Pingback: A journey of SAS high performance - SAS Users

  9. Pingback: Reproducible research: Is my SAS code enough? - SAS Voices

  10. Chris, I currently teach a Statistical Computing course using SAS, I migrated myself and my students over to using SAS EG as a programming interface, but I am wondering if this is the best way to go? Do you have a sense of what is being used as the programming interface in industry?

    Should I be switching to a notebook approach, or is SAS studio a better way to go?

    I have multiple courses I teach and I will be developing a first course for a new Data Science masters that uses SAS and I wonder if I should be using SAS-EG, SAS Studio or learning these notebooks? I have noticed that workflow is something students need to learn, instead of figuring it out over the years and that the programming interface has an impact on learning and I wanted to get a sense of which method to use as I go forward.

    Thanks.

    Laura

  11. No SAS libraries or datasets to be explored (don't make mistakes), no syntax highlighting (don't make mistakes), the log will appear in a squished box with a scroll bar (don't make mistakes). Another tiny scroll box for results (don't worry, I'm sure your results are fine).

    Just ONE of the incredible replacements for Enterprise Guide coming to a SAS shop near you!

    • Chris Hemedinger
      Chris Hemedinger on

      Sarcasm duly noted ;)

      Well, look, Jupyter Notebook is not for everyone, and for some of us who are accustomed to more sophisticated, interactive environments -- it does seem like a step backward into the console-only days. I think of this as the hipster coding environment. And if there is one thing that I know about hipsters, it's that they don't like it when tasks are too easy or straightforward. There's no school like the old school, after all.

      However, Jupyter Notebook does have support for dozens of popular coding languages, and it treats them all in a similar (albeit simple) way. So it's one more option for Jupyter Notebook users who find themselves needing SAS analytics.

  12. Christian Setzkorn on

    Thanks for this! You do not seem to be able to use macros though. Is this correct? Cannot use this code:

    %macro SomeExcitingMacro(OriginName);
    PROC SGPLOT DATA=SASHELP.cars(where=(Origin = "&OriginName."));
    VBOX Invoice
    / category = DriveTrain nooutliers;
    yaxis min=0 max=100000;
    RUN;
    %mend SomeExcitingMacro;

    proc sql;
    create table LoopData as
    select
    distinct Origin
    from SASHELP.cars
    ;
    quit;

    data _null_;
    set LoopData;
    call execute('%nrstr(%SomeExcitingMacro('||Origin||'))');
    run;

    • Chris Hemedinger
      Chris Hemedinger on

      Any SAS code should work, including macros. The % and %% chars have special meaning in Jupyter, so something might need to be escaped there. I'll have to test this particular program -- but in general, it should work.

  13. Hi,I find a problem in my jupyter. I cannot solve it.

    when I submit this code.
    %%SAS
    proc print data=sashelp.class;
    run;

    data work.a;
    set sashelp.cars;
    run;

    the result shows?

    SAS ϵͳ

    Obs Name Sex Age Height Weight
    1 °¢¶û¸¥À׵ ÄÐ 14 69.0 112.5
    2 °®ÀöË¿ Å® 13 56.5 84.0
    3 °Å°ÅÀ­ Å® 13 65.3 98.0
    4 ¿­Â¶ Å® 14 62.8 102.5
    5 ºàÀû ÄÐ 14 63.5 102.5
    6 ղķ˹ ÄÐ 12 57.3 83.0
    7 ¼ò Å® 12 59.8 84.5
    8 ÑÅÄÝÌØ Å® 15 62.5 112.5

    • Chris Hemedinger
      Chris Hemedinger on

      This looks like you might be using a Chinese version of SAS, but running with an encoding that doesn't match with Jupyter. The best practice is to use UTF8 Encoding options when you launch SAS, which you can configure in your SAS startup command or in your sasv9.cfg.

      • But the data is English.
        Name Sex Age Height Weight
        Alfred M 14 69 112.5
        Alice F 13 56.5 84
        Barbara F 13 65.3 98
        Carol F 14 62.8 102.5
        Henry M 14 63.5 102.5
        James M 12 57.3 83
        Jane F 12 59.8 84.5
        Janet F 15 62.5 112.5
        Jeffrey M 13 62.5 84
        John M 12 59 99.5
        Joyce F 11 51.3 50.5
        Judy F 14 64.3 90
        Louise F 12 56.3 77
        Mary F 15 66.5 112
        Philip M 16 72 150
        Robert M 12 64.8 128
        Ronald M 15 67 133
        Thomas M 11 57.5 85
        William M 15 66.5 112
        I try use my utf8 sasv9.cfg to replace zh sasv9.cfg. But the problem still exist.
        I only find the sasv9.cfg in "C:\Program Files\SASHome\SASFoundation\9.4\nls".
        Is this the file location?

        • Chris Hemedinger
          Chris Hemedinger on

          Some sample data that ships with SAS is localized, and can be different if using a different locale. Check the PROC CONTENTS for the SASHELP.CLASS to verify that the file is coming from an English directory. Use PROC OPTIONS to see where your config file is being read from. The sasv9.cfg is either in the same path as SAS.exe (and that will be a small file that simply references a locale-specific config), or in a larger SAS deployment this will be in your Config/Lev1 folder.

          • Thank you for your help. When I modify my cfg. The problem is disappear. I can see the English data.
            But I have another problem, sorry.
            %%SAS
            ods html5 style = statistical;
            ods graphics /width =500 height =400;
            proc means data=sashelp.cars;run;

            the tile is that doesn't match with Jupyter.
            like ��

            æ ‡ç­¾
            I cannot send my screenshot to you..

Leave A Reply

Back to Top