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;
""")
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
@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.
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
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
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.