Python Forum
SAS Equivalent of Macro variable in Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
SAS Equivalent of Macro variable in Python
#1
I am writing code to simulate different scenarios. However, it gets cumbersome to type in the values for different scenarios. I was hoping I could use something similar to a SAS Macro variable so that I can change the values at the top and the bottom would invoke (via the macro variable) the changes

I've tried various options but no luck

/* Defining the Macro Variable */
%LET a = 10;
%LET b = 5;

/* Invoking it */
x = &a + &b
The answer is 15

thanks

PC
Reply
#2
I must be missing something here. Why do variables not work for your project?

a = 10
b = 5

x = a + b
print(x)
Reply
#3
You don't need macro variables in Python. Python is much more flexible than SAS.

a = 10
b = 5
x = a + b
print(x)   # prints 15
If you want to run the same code for different scenarios, you should put that code into a function or a class, and use parameters:

def add(a, b):
    return a + b
x = add(5, 10)
print(x)        # prints 15
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#4
thank you ichabod801. Very helpful.

PC
Reply
#5
SAS macro is much more flexible that the construction mentioned above in Python.
SAS macro variable is taken any text. Like
%let a=this is a text;
contents of a is string between '=' and ';'
Then it is converted into code after processing part of multi-line code text, containing
referencing &a.
in Python so far I have found the way only to use variables defined in Python in expressions
used for data frames with referencing @var_name.
For example, very easy task to filter some data frame on columns of type object
which works in code like this

adsl[(adsl['sex']==b'1') & (adsl['area']==b'2') ]

If I need to specify the values for selection somewhere before this code, there is no way to do this.
While the following code is working well
age_min=25
age_max=64
year_min=1972
year_max=2002
adsl1.query( "(@age_min<=age<=@age_max)  & (@year_min<=year<=@year_max)") 
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Equivalent Python code from VBA Mishal0488 1 631 Jul-05-2023, 10:43 AM
Last Post: carecavoador
Question convert autohotkey script to python / macro - press key when pixel get colour willson94d 1 3,572 Jan-01-2022, 08:13 PM
Last Post: Yoriz
  Python C++ Template Equivalent deanhystad 7 3,312 May-04-2021, 07:45 PM
Last Post: deanhystad
  Auto-populate Macro variables Spartan314 3 2,608 Mar-08-2021, 12:36 AM
Last Post: Spartan314
  How do I reduce the time to Invoke Macro via Python? JaneTan 1 2,074 Dec-28-2020, 06:46 AM
Last Post: buran
  Hi Guys, please help me to write SAS macro parameter equivalent code in Python Manohar9589 2 2,536 Jun-14-2020, 05:07 PM
Last Post: Larz60+
  python equivalent to MATLAB xcov chai0404 2 3,816 Apr-02-2020, 10:29 PM
Last Post: chai0404
  Python equivalent of Matlab code kwokmaster 1 3,398 Mar-25-2020, 10:14 PM
Last Post: j.crater
  Equivalent of 'bwareafilt' for Python Mighty 1 3,540 Feb-05-2020, 10:32 PM
Last Post: Marbelous
  Jython macro which uses PythonInterpreter and Redis script gives an error rkanumola 2 2,204 Oct-30-2019, 07:37 AM
Last Post: rkanumola

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020