Hi,
Has anyone coded alternative for SAS Macros in python?
How can we develop SAS macro variables in Python?
Thanks.
SAS and Python are totally different programming languages???
You can pass parameters into a SAS code, but would need to know a lot more information about what is driving one or the other, how its running etc.
Hey,
I have a block of code written in SAS programming using SAS macros.
I am trying to replicate the same functionality using PySpark, So I was thinking of writing a python function to replicate the logic.
Am i good here or is there anything in Python which is equivalent to SAS macros?
Sorry, going to repeat, Python and SAS are two different programming languages. If you have code in one, there is no automatic convert from one to the other, any more than there is in any other language. You would need to code the logic from one using the syntax and structures of the other.
As for your other point, there are similar things like macro in other languages such as Python. However its not completely the same. Macro is simply a text generator. If the text generated is valid SAS code, then that passes through the SAS compiler and is processed. In Python there are packages for text generation - such as building generic code generated HTML pages, but something which exactly replicates what SAS macro does then no, not as far as I am aware.
Do you know how to run the SAS macros, using SAS? (Or do you know somebody who could do that for you?)
If you were developing a new SAS macro, the typical process would be to construct a working SAS program that does the job, but needs to be generalized by adding flexible parameters. I would suggest mirroring that process.
It's work, but I have seen it done this way (with Python, rather than PySpark).
@kolanpaka wrote:
Am i good here or is there anything in Python which is equivalent to SAS macros?
A python package/library is the equivalent of a SAS macro, in terms of functionality and how it works.
There is no automated way to convert a SAS macro to a Python script, your best bet is to deconstruct the logic and then implement that in python using the python approach to optimize things.
If you're using SAS Viya there are ways to use Python with SAS, but I suspect that's not what you're looking for. If you don't understand the SAS code, consider hiring a consultant to either provide the logic or do the conversion. Alternatively, the first two SAS programming courses are free - but macros are usually more complicated than those and those are considered advanced tasks.
It happens that many SAS programs are indeed a lot of chained PROC SQL.
It is quite easy in this case to use PySpark SQL feature (spark.sql()) to reuse these PROC SQL.
Of course SAS SQL is not compliant to SQL standard and you will have to adapt some functions / find the equivalent.
But you can leverage the logic, I have done it few times.
If your SAS program does not use PROC SQL or for the other parts of your program, you will need to adapt and translate...
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.