BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
michelconn
Quartz | Level 8

Can some one please give me an example of symget and symput from saspy? I read over the section in advanced topics but I don't understand how to create a SAS macro variable that is recognized by python. I've created a working macro variable using the submit command but it was only recognized within the submit command. Is it possible to create a SAS macro variable within the submit command and pass the value to a python variable? I can image using a CSV or the SAS log to do it but that seems clunky.

 

c = sas.submit("""
%let sal=salary;
data low;
    set _csv;
    if &sal='low' then output;
run;

""")

1 ACCEPTED SOLUTION

Accepted Solutions
sastpw
SAS Employee

Hey, just saw this, and I have to say, I really didn't provide a useful example of this in that Advanced topics section 😞
So, I just added a sample notebook to the saspy-examples site showing how you can use this.

 

https://github.com/sassoftware/saspy-examples/blob/master/SAS_contrib/Using_SYMGET_and_SYMPUT.ipynb

 

For this post, I can run in line mode and show the same thing here too;The notebook is prettier though 🙂 

 

tom64-3> python3.5
Python 3.5.5 (default, Feb 6 2018, 10:56:47)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-18)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import saspy
>>> sas = saspy.SASsession(cfgname='sdssas')
SAS Connection established. Subprocess id is 28930

>>> sas
Access Method = STDIO
SAS Config name = sdssas
WORK Path = /sastmp/SAS_workC57A00007120_tom64-3/
SAS Version = 9.04.01M4D11092016
SASPy Version = 2.2.9
Teach me SAS = False
Batch = False
Results = Pandas
SAS Session Encoding = WLATIN1
Python Encoding value = cp1252

>>> sas.symput('new_var', 52)
>>> sas.symget('new_var')
52
>>> sas.symput('nuther_var', 'Hi there')
>>>
>>> sas.symget('nuther_var')
'Hi there'
>>>
>>> py_var = sas.symget('nuther_var')
>>> print(py_var)
Hi there

>>> sas.symput('from_py_var', py_var+" y'all")
>>> print(sas.symget('from_py_var'))
Hi there y'all
>>>

Hope this helps!

Tom

View solution in original post

4 REPLIES 4
ballardw
Super User

@michelconn wrote:

Can some one please give me an example of symget and symput from saspy? I read over the section in advanced topics but I don't understand how to create a SAS macro variable that is recognized by python. I've created a working macro variable using the submit command but it was only recognized within the submit command. Is it possible to create a SAS macro variable within the submit command and pass the value to a python variable? I can image using a CSV or the SAS log to do it but that seems clunky.

 

c = sas.submit("""
%let sal=salary;
data low;
    set _csv;
    if &sal='low' then output;
run;

""")


Much better, and likely easier, than "images" of the log is to copy and paste text directly from the log into a code window opened using the forum {I} icon.

And things involving macros will generally have much more usable detail if:

options mprint symbolgen ;

is run prior to anything involving macros.

sastpw
SAS Employee

Hey, just saw this, and I have to say, I really didn't provide a useful example of this in that Advanced topics section 😞
So, I just added a sample notebook to the saspy-examples site showing how you can use this.

 

https://github.com/sassoftware/saspy-examples/blob/master/SAS_contrib/Using_SYMGET_and_SYMPUT.ipynb

 

For this post, I can run in line mode and show the same thing here too;The notebook is prettier though 🙂 

 

tom64-3> python3.5
Python 3.5.5 (default, Feb 6 2018, 10:56:47)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-18)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import saspy
>>> sas = saspy.SASsession(cfgname='sdssas')
SAS Connection established. Subprocess id is 28930

>>> sas
Access Method = STDIO
SAS Config name = sdssas
WORK Path = /sastmp/SAS_workC57A00007120_tom64-3/
SAS Version = 9.04.01M4D11092016
SASPy Version = 2.2.9
Teach me SAS = False
Batch = False
Results = Pandas
SAS Session Encoding = WLATIN1
Python Encoding value = cp1252

>>> sas.symput('new_var', 52)
>>> sas.symget('new_var')
52
>>> sas.symput('nuther_var', 'Hi there')
>>>
>>> sas.symget('nuther_var')
'Hi there'
>>>
>>> py_var = sas.symget('nuther_var')
>>> print(py_var)
Hi there

>>> sas.symput('from_py_var', py_var+" y'all")
>>> print(sas.symget('from_py_var'))
Hi there y'all
>>>

Hope this helps!

Tom

sastpw
SAS Employee

And, not to make you think that you can only use these methods by together themselves, No, you can intermix them with your SAS code, and so all the things you would expect. You can get at macro variables already in SAS with symget() and you can use macro variables created with symput() in your SAS code too. Here a few more lines showing that (hope the output from head() formats correctly on this):

 

 >>> ll = sas.submit('''
... %let what_happened=it worked!;
...
... data a; x=symget('from_py_var'); y=symget('new_var');run;
... ''')
>>>
>>> data_a = sas.sasdata('a', results='text')
>>>
>>> data_a.head()
The SAS System

Obs          x                y

1     Hi there y'all      52
>>>
>>>
>>> print(sas.symget('what_happened'))
it worked!
>>>
>>>
 

Thanks!

Tom

michelconn
Quartz | Level 8
Thanks for the follow up, that explanation makes a lot of sense.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 4 replies
  • 3060 views
  • 0 likes
  • 3 in conversation