SAS to Python Translation
December 1, 2019
Recently, I have some opportunities to use both SAS and python programming skills. It’s not a coincidence that these two languages have something in common. This research is trying to find a general formula for a one to one translation. Here is an example of SQL statements.
SAS codes | Python3 codes |
---|---|
PROC SQL; CREATE TABLE new AS SELECT * FROM airports; Quit; |
import pandas as pd new = airports.copy(deep=True) |
SELECT * FROM airports WHERE airports.iso_country = 'US'; | airports[airports.iso_country == 'US'] |
SELECT id,type FROM airports WHERE airports.iso_country = 'US'; |
airports[airports.iso_country == 'US'][['id','type']] |
SELECT type,count(*) FROM airports WHERE airports.iso_country = 'US' GROUP BY type; |
airports[airports.iso_country == 'US'].groupby('type').size().reset_index() |
SELECT type,count(*) AS size,min(id) AS id_min FROM airports WHERE airports.iso_country = 'US' GROUP BY type HAVING count(*)>1000; |
airports[airports.iso_country == 'US'].groupby('type').filter(lambda x:len(x)>1000).groupby('type').agg(size = ('id','count'),id_min = ('id','min')).reset_index() |
References:
• Pandas - Comparison with SAS
• How to rewrite your SQL queries in Pandas, and more