BookmarkSubscribeRSS Feed
kolanpaka
Calcite | Level 5

Hi,

 

Has anyone coded alternative for SAS Macros in python?

How can we develop SAS macro variables in Python?

 

Thanks.

6 REPLIES 6
RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

kolanpaka
Calcite | Level 5

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?

 

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.  

Astounding
PROC Star

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.

 

  1. With the help of a programmer familiar with SAS macros, run the macro.  Use the MFILE / MPRINT options to obtain a copy of the source code generated, with all the macro language removed.
  2. Build a PySpark function to replicate this cast-in-concrete version of the program.
  3. Add flexibility to the PySpark function to support any needed flexibility.

It's work, but I have seen it done this way (with Python, rather than PySpark).

Reeza
Super User

@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. 

Herve_Mignot
Calcite | Level 5

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...

 

SAS Innovate 2025: Call for Content

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!

Submit your idea!

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
  • 6 replies
  • 19515 views
  • 1 like
  • 5 in conversation