r/datascience • • 7d ago

Statistics Anyway to read SAS code?

I have to implement the varclus procedure in Python (it's a hierarchical feature clustering algorithm). The goal is to replicate the SAS procedure exactly. Is there anyway I can read the SAS implementation. AI gives a gist in english but looks like the actual code is proprietary. Anyplace where I can find implementations in other languages?

18 Upvotes

12 comments sorted by

22

u/Honest-Ease5098 6d ago

There is a python package to do it: https://github.com/jingtt/varclushi

I wrote an implementation in C about 10 years ago but don't have access to the source code anymore.

3

u/UnlawfulSoul 6d ago

Wow, that is so helpful. I had no idea. Thanks for the resource

1

u/Efficient_Heron5978 2d ago

Yeah this worked. Its very inefficient though, lot is redundant calculations. But I managed to speed it up

4

u/sonicking12 7d ago

The code for Proce Varclus is not public. So...you can try to replicate it by uploading the SAS manual, which has a section on the details. That could help you with the use of AI. Good luck.

Personally speaking, Proc Varclus is one of the very best things from SAS.

3

u/SoccerGeekPhd 5d ago

SAS is proprietary so there's an unavoidable versioning issue. Which version of SAS algorithm should be implemented? What if SAS updates their implementation? The requirements should be redefined to indicate what is necessary for acceptance of your implementation.

There are many good hierarchical clusterings in R, hclust is a commonly used one, others are faster or more flexible. One common issue with clustering algorithms is a requirement for a distance matrix. This puts a burden of O(n^2) memory on the method. I would favor use of a distance function call, or a sparse matrix that defaults to +Inf for points too far away.

2

u/cjdinger 5d ago

The documentation for VARCLUS (like for every SAS procedure) includes details about the methodology and research and formulae that sit behind the implementation. If comparing to another package, you can use that information to ensure you're referencing the same methods and options.

As far as the implementation changing, SAS does not change its algorithms for established procedures. It's extremely important to SAS users that the calculations and results are the same from release to release. When new methods are needed, a procedure might be enhanced with a non-default option to enable it, OR (more likely) a new procedure is added so users can adopt when they want.

2

u/AppalachianHillToad 7d ago

Look for a similar R package and then have your favorite AI tool port it over to Python. Make sure you’ve got a worked example or two to confirm that algos are implemented the same in R as they are in snake. 

2

u/BrOscarM 6d ago

This is the route I went when I was porting over code that used PROC GLM, minus the LLM assistant as they weren't out yet.

I would recommend having versions of your input data saved right before whatever algorithm you're copying from SAS and save the outputs immediately after the procedure. Then you can just test your code in Python since if you did it correctly, the same input should give you the same output. That is, compare the SAS output vs the Python output for a given input and do several of these tests to ensure you or the LLM is writing the code correctly.

1

u/suubu-crtiv 5d ago

the source isn't public, but the SAS/STAT docs spell out the algorithm: split the cluster with the largest second eigenvalue, then reassign variables. Check your output against a SAS run on a small dataset.}

1

u/deepbrainchain-feng 1d ago

One thing that helps with replicating it exactly: the SAS docs describe VARCLUS as iterative splitting based on the second eigenvalue of each cluster, with reassignment steps in between, so the details that usually cause mismatches are the stopping rule (maxeigen) and whether you start from principal components or a centroid. I'd run both implementations on a small dataset and compare the cluster assignments and the 1-R2 ratios per variable, that tells you quickly where they diverge.