BookmarkSubscribeRSS Feed
braam
Quartz | Level 8

Thanks to the SAS blog (https://blogs.sas.com/content/sasdummy/2016/04/24/how-to-run-sas-programs-in-jupyter-notebook/), I'm trying to use SAS in Jupyter Notebook. But I would like to know how I can recall and run another SAS code file in Jupyter Notebook.

 

Back in my Windows SAS, I always have the below line at the beginning of all my code files.

 

%include ".\1. Research\MacroSetup.sas";

Please note that I used a relative path in %include statement, so it starts with "."

 

 

I wanted to achieve the same thing in Jupyter, but it didn't work. Could anybody please help me with that? Even though I would like to keep using the relative path, it is not necessary. So I replaced it with its full path, but it didn't work.

In addition, I also replaced "\" with "\\" in Jupyter, but it didn't work.

 

The log I got is here:

Error in calling magic 'include' on line:
    [Errno 22] Invalid argument: 'C:\\Dropbox (Personal)\\1. Research\\0 Common sets\\Compustat\\.\x01'
    args: ['.\x01. Research\\MacroSetup.sas ;']
    kwargs: {}
Traceback (most recent call last):
  File "C:\Users\yjaey\Anaconda3\lib\site-packages\metakernel\magic.py", line 94, in call_magic
    func(*args, **kwargs)
  File "C:\Users\yjaey\Anaconda3\lib\site-packages\metakernel\magics\include_magic.py", line 30, in line_include
    text += open(filename).read() + "\n"
OSError: [Errno 22] Invalid argument: 'C:\\Dropbox (Personal)\\1. Research\\0 Common sets\\Compustat\\.\x01'

%include FILENAME ... - include code from filename into this code

This line magic will get the contents of a file and include it
in this cell evaluation.

You can have multiple %include's at the beginning of a cell,
and they will be included in order.

Examples:
    %include myprog.py
    %include myprog1.py myprog2.py

 

In addition to this macro setup, many of my SAS codes are inter-related with %include statement.  so, I really would like to figure this out. Many thanks.

3 REPLIES 3
Kalind_Patel
Lapis Lazuli | Level 10

Jupyter is always sensitive for '%', 

If you need to run a SAS macro as the first statement, then insert a blank line as the first line in the notebook cell and the macro on the second line. The blank line prevents Python from interpreting the macro variable as a magic.

braam
Quartz | Level 8
@Kalind_Patel
Thanks a lot. It worked! Relatedly, could you please also let me know how to run another ipynb code in my MAIN ipynb code file that works with SAS kernel in Jupyter?

It's basically the same situation as I described with the exception that the target code is ipynb (instead of sas) because I'll write down my SAS code in Jupyter.

Kalind_Patel
Lapis Lazuli | Level 10

I'm not sure what exactly you want to achieve by running another ipynb file,
but I guess it should be executed like this:

%run ./another_file.ipynb
foo()

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 674 views
  • 0 likes
  • 2 in conversation