Enterprise Healthcare SAS-to-Python Conversion
As one of two Python developers, I converted a client's SAS programs to Python, then built AI conversion agents based on what I learned.
TL;DR: I was one of two Python developers who converted a client's SAS programs to Python, and the client validated and accepted them. We sanitized the code and logs before they left the client's environment, wrote tested functions to match SAS results, and cut one large section from 181 minutes in an earlier Python version to 62. I then built AI conversion agents on Microsoft Foundry based on what I learned.
My role
I was one of two Python developers on the conversion, and we used an AI coding assistant to do the conversion work.
Process
We followed the same steps for each conversion:
- Sanitize code and logs before they leave the client's environment.
- List every macro and input the programs needed, and ask the SAS developer, who had access to the environment, for anything missing.
- Write reference documents describing what the SAS programs do.
- Convert, test and review the code.
- Build a versioned release.
- Compare the Python output with the SAS output, fix the differences and repeat.
Architecture
For each conversion, we kept a separate program with its own tests, settings and release notes. We put code needed by more than one conversion, including Python versions of SAS functions, in a shared and versioned library.
We used DuckDB for SQL, pandas for data logic and pytest for tests. In each program, we read the inputs in one place and wrote the outputs in another, and used plain functions to transform the tables in between. We kept settings in a configuration file.
Challenges
Sanitizing code and logs
We needed the code and logs to do the conversion, but we couldn't let sensitive details leave the client's environment. We built a sanitization tool and used it to replace values such as server names, file paths, email addresses and passwords with placeholders. For example, if two programs read from the same folder, we used the sanitization tool to replace its path with the same placeholder, such as &value1, in both. We couldn't see the real path, but we could still tell that both programs were reading from the same place.
We shared only the cleaned copies and left the original files unchanged. We kept the mappings and replacement reports private because they contained the real values. On the machine used to run the Python, we could use the mappings to fill those values back in.
We didn't rely on the sanitization tool alone, since it could miss things. We also read the cleaned files and followed a checklist that covered personal and health information, credentials, infrastructure details, internal comments and logs. When we found something the sanitization tool had missed, we added a rule for it.
No access to the environment
Under the agreement, the Python developers didn't have access to the production environment or its data, so we risked redoing work if we missed a macro or guessed wrong about a file. We mapped dependencies before converting anything, our rule was to ask for the real structure of an input file instead of guessing it, and we tested with made-up data.
Differences between SAS and Python
When we translated SAS code line by line, the Python could run without errors and still give different results. These are some of the differences:
- In SAS, two blank values are equal, but in a standard SQL join, two nulls don't match.
- When a key repeats, a SAS MERGE matches the rows in order, but a SQL join matches every row with every row that has the same key, so you get extra rows.
- You get 3 when you round 2.5 in SAS, and 2 with Python's built-in round.
- In DuckDB, you can't rely on row order without an explicit sort.
- When you add decimal numbers on several threads, you can get different last digits, so you need to compare with a tolerance.
For differences like these, we wrote a tested Python function in the shared library that returns the same result as SAS.
Matching the SAS output
We couldn't rely on passing tests to prove that the Python matched the SAS, and by checking only values we could miss different column names or types. We built a comparison tool and used it to check columns, row counts, missing values, types and values. For a fair comparison, we also needed data from the same point in time for both runs.
Speed and memory
Some programs were much slower and used far more memory than we expected, because we fetched data into Python and held large tables in memory. We measured where the time went before changing the code. To fix this, we fetched fewer columns, moved joins into the database, used Parquet files with DuckDB and ran independent work in parallel. By making such optimizations, we cut one large section from 181 minutes in an earlier Python version to 62.
Shared code
Because we could affect more than one conversion by changing the shared library, we versioned it. We added an automated check to block library changes without a new version.
Result
The client validated and accepted the converted programs.
AI conversion agents
At the end of the project, I used what I'd learned to build AI agents on Microsoft Foundry. I built an orchestrator to start a separate agent session for each stage: discovery, conversion, validation and release.
I didn't want the conversion agent to check its own work, because it could repeat its own wrong assumptions. I built a separate validation agent to review the code and compare the outputs. For each finding, I require it to show the code involved, an example input and the result from each language.
I also require a person to accept discovery before conversion starts, and to accept validation before release. I designed the confirmation prompt so that the model can't answer it.
For the release stage, I have the code rerun in a clean environment and the output files compared byte for byte. In testing, I used this check to catch a library that was missing from the requirements.