Moe Chizari

Projects

The write-up

Interactive demo, added

Sas2py – Migration is the only way from SAS to Python: try it.

Type a few lines of SAS and see the matching Python (pandas) code. A teaching converter for a small part of SAS.

Try it

Move the controls; the chart and the sentence under it update.

Python with pandas, updated as you type:

import io
import pandas as pd

# 50 states, from the U.S. Census Bureau's Statistical Abstract of the
# United States and County and City Data Book (1977), as kept in R's
# datasets package (state.x77, state.region, state.abb).
# population: thousands, July 1, 1975; income: per capita, 1974;
# life_exp: years, 1969-71; hs_grad: percent high-school graduates, 1970;
# area: land area, square miles
# DATA step: states
states_lines = """\
AL,South,3615,3624,69.05,41.3,50708
AK,West,365,6315,69.31,66.7,566432
AZ,West,2212,4530,70.55,58.1,113417
AR,South,2110,3378,70.66,39.9,51945
CA,West,21198,5114,71.71,62.6,156361
CO,West,2541,4884,72.06,63.9,103766
CT,Northeast,3100,5348,72.48,56,4862
DE,South,579,4809,70.06,54.6,1982
FL,South,8277,4815,70.66,52.6,54090
GA,South,4931,4091,68.54,40.6,58073
HI,West,868,4963,73.6,61.9,6425
ID,West,813,4119,71.87,59.5,82677
IL,North Central,11197,5107,70.14,52.6,55748
IN,North Central,5313,4458,70.88,52.9,36097
IA,North Central,2861,4628,72.56,59,55941
KS,North Central,2280,4669,72.58,59.9,81787
KY,South,3387,3712,70.1,38.5,39650
LA,South,3806,3545,68.76,42.2,44930
ME,Northeast,1058,3694,70.39,54.7,30920
MD,South,4122,5299,70.22,52.3,9891
MA,Northeast,5814,4755,71.83,58.5,7826
MI,North Central,9111,4751,70.63,52.8,56817
MN,North Central,3921,4675,72.96,57.6,79289
MS,South,2341,3098,68.09,41,47296
MO,North Central,4767,4254,70.69,48.8,68995
MT,West,746,4347,70.56,59.2,145587
NE,North Central,1544,4508,72.6,59.3,76483
NV,West,590,5149,69.03,65.2,109889
NH,Northeast,812,4281,71.23,57.6,9027
NJ,Northeast,7333,5237,70.93,52.5,7521
NM,West,1144,3601,70.32,55.2,121412
NY,Northeast,18076,4903,70.55,52.7,47831
NC,South,5441,3875,69.21,38.5,48798
ND,North Central,637,5087,72.78,50.3,69273
OH,North Central,10735,4561,70.82,53.2,40975
OK,South,2715,3983,71.42,51.6,68782
OR,West,2284,4660,72.13,60,96184
PA,Northeast,11860,4449,70.43,50.2,44966
RI,Northeast,931,4558,71.9,46.4,1049
SC,South,2816,3635,67.96,37.8,30225
SD,North Central,681,4167,72.08,53.3,75955
TN,South,4173,3821,70.11,41.8,41328
TX,South,12237,4188,70.9,47.4,262134
UT,West,1203,4022,72.9,67.3,82096
VT,Northeast,472,3907,71.64,57.1,9267
VA,South,4981,4701,70.08,47.8,39780
WA,West,3559,4864,71.72,63.5,66570
WV,South,1799,3617,69.48,41.6,24070
WI,North Central,4589,4468,72.48,54.5,54464
WY,West,376,4566,70.29,62.9,97203
"""
states = pd.read_csv(io.StringIO(states_lines), header=None,
                     names=['state', 'region', 'population', 'income', 'life_exp', 'hs_grad', 'area'],
                     dtype={'state': str, 'region': str}, na_values='.')

# PROC SORT
# Note: SAS sorts missing values first in ascending keys and last in descending keys
states = (states.sort_values('income', ascending=False, kind='stable')
    .sort_values('region', na_position='first', kind='stable')
    .reset_index(drop=True))

# PROC MEANS
print(states.groupby('region')[['income', 'life_exp', 'hs_grad']].agg(['count', 'mean', 'min', 'max']).round(2).to_string())

# PROC FREQ
freq = states['region'].value_counts().sort_index()
print(pd.DataFrame({'Frequency': freq, 'Percent': 100 * freq / freq.sum(),
                    'Cumulative Frequency': freq.cumsum(),
                    'Cumulative Percent': 100 * freq.cumsum() / freq.sum()}).to_string())

# PROC PRINT
print(states.head(5).to_string())

SAS listing, by SAS's rules (SAS itself does not run on this page).

                       The MEANS Procedure

                  N
region          Obs   Variable    N      Mean   Minimum   Maximum
-----------------------------------------------------------------
North Central    12   income     12   4611.08   4167.00   5107.00
                      life_exp   12     71.77     70.14     72.96
                      hs_grad    12     54.52     48.80     59.90

Northeast         9   income      9   4570.22   3694.00   5348.00
                      life_exp    9     71.26     70.39     72.48
                      hs_grad     9     53.97     46.40     58.50

South            16   income     16   4011.94   3098.00   5299.00
                      life_exp   16     69.71     67.96     71.42
                      hs_grad    16     44.34     37.80     54.60

West             13   income     13   4702.62   3601.00   6315.00
                      life_exp   13     71.23     69.03     73.60
                      hs_grad    13     62.00     55.20     67.30
-----------------------------------------------------------------

pandas output, what print() shows for the converted code:

              income                      life_exp                      hs_grad                   
               count     mean   min   max    count   mean    min    max   count   mean   min   max
region                                                                                            
North Central     12  4611.08  4167  5107       12  71.77  70.14  72.96      12  54.52  48.8  59.9
Northeast          9  4570.22  3694  5348        9  71.26  70.39  72.48       9  53.97  46.4  58.5
South             16  4011.94  3098  5299       16  69.71  67.96  71.42      16  44.34  37.8  54.6
West              13  4702.62  3601  6315       13  71.23  69.03  73.60      13  62.00  55.2  67.3

All 18 SAS statements converted. Run on its 50 data lines, the SAS listing and the pandas output agree on all 99 values they show. PROC MEANS, by region: the highest mean of income is West (4,702.62) and the lowest is South (4,011.94).

Data: U.S. Census Bureau, Statistical Abstract of the United States: 1977 and County and City Data Book, 1977, as kept in R's datasets package (state.x77, state.region and state.abb, read from the Rdatasets files on 15 October 2026). Population: thousands, July 1, 1975. Income: per capita, 1974. Life expectancy: 1969 to 1971. High-school graduates: percent, 1970. Area: land area, square miles.

A teaching converter for a small part of SAS (DATA step basics; PROC SORT, MEANS, FREQ and PRINT), written as fixed rules that run in your browser. Both outputs are computed on this page from the program's data lines: the SAS listing follows SAS's rules and imitates its layout, but SAS does not run here; the pandas output is what the converted code prints with pandas 3.0, checked against Python when the page was built. It is not a production migration tool: real migrations need the output checked against SAS results.

The write-up this goes with: Sas2py – Migration is the only way from SAS to Python.

All demos