1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584
|
.. _compare_with_sas:
{{ header }}
Comparison with SAS
********************
For potential users coming from `SAS <https://en.wikipedia.org/wiki/SAS_(software)>`__
this page is meant to demonstrate how different SAS operations would be
performed in pandas.
.. include:: includes/introduction.rst
Data structures
---------------
General terminology translation
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. csv-table::
:header: "pandas", "SAS"
:widths: 20, 20
``DataFrame``, data set
column, variable
row, observation
groupby, BY-group
``NaN``, ``.``
``DataFrame``
~~~~~~~~~~~~~
A ``DataFrame`` in pandas is analogous to a SAS data set - a two-dimensional
data source with labeled columns that can be of different types. As will be
shown in this document, almost any operation that can be applied to a data set
using SAS's ``DATA`` step, can also be accomplished in pandas.
``Series``
~~~~~~~~~~
A ``Series`` is the data structure that represents one column of a
``DataFrame``. SAS doesn't have a separate data structure for a single column,
but in general, working with a ``Series`` is analogous to referencing a column
in the ``DATA`` step.
``Index``
~~~~~~~~~
Every ``DataFrame`` and ``Series`` has an ``Index`` - which are labels on the
*rows* of the data. SAS does not have an exactly analogous concept. A data set's
rows are essentially unlabeled, other than an implicit integer index that can be
accessed during the ``DATA`` step (``_N_``).
In pandas, if no index is specified, an integer index is also used by default
(first row = 0, second row = 1, and so on). While using a labeled ``Index`` or
``MultiIndex`` can enable sophisticated analyses and is ultimately an important
part of pandas to understand, for this comparison we will essentially ignore the
``Index`` and just treat the ``DataFrame`` as a collection of columns. Please
see the :ref:`indexing documentation<indexing>` for much more on how to use an
``Index`` effectively.
Copies vs. in place operations
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
.. include:: includes/copies.rst
Data input / output
-------------------
Constructing a DataFrame from values
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
A SAS data set can be built from specified values by
placing the data after a ``datalines`` statement and
specifying the column names.
.. code-block:: sas
data df;
input x y;
datalines;
1 2
3 4
5 6
;
run;
.. include:: includes/construct_dataframe.rst
Reading external data
~~~~~~~~~~~~~~~~~~~~~
Like SAS, pandas provides utilities for reading in data from
many formats. The ``tips`` dataset, found within the pandas
tests (`csv <https://raw.githubusercontent.com/pandas-dev/pandas/main/pandas/tests/io/data/csv/tips.csv>`_)
will be used in many of the following examples.
SAS provides ``PROC IMPORT`` to read csv data into a data set.
.. code-block:: sas
proc import datafile='tips.csv' dbms=csv out=tips replace;
getnames=yes;
run;
The pandas method is :func:`read_csv`, which works similarly.
.. ipython:: python
url = (
"https://raw.githubusercontent.com/pandas-dev/"
"pandas/main/pandas/tests/io/data/csv/tips.csv"
)
tips = pd.read_csv(url)
tips
Like ``PROC IMPORT``, ``read_csv`` can take a number of parameters to specify
how the data should be parsed. For example, if the data was instead tab delimited,
and did not have column names, the pandas command would be:
.. code-block:: python
tips = pd.read_csv("tips.csv", sep="\t", header=None)
# alternatively, read_table is an alias to read_csv with tab delimiter
tips = pd.read_table("tips.csv", header=None)
In addition to text/csv, pandas supports a variety of other data formats
such as Excel, HDF5, and SQL databases. These are all read via a ``pd.read_*``
function. See the :ref:`IO documentation<io>` for more details.
Limiting output
~~~~~~~~~~~~~~~
.. include:: includes/limit.rst
The equivalent in SAS would be:
.. code-block:: sas
proc print data=df(obs=5);
run;
Exporting data
~~~~~~~~~~~~~~
The inverse of ``PROC IMPORT`` in SAS is ``PROC EXPORT``
.. code-block:: sas
proc export data=tips outfile='tips2.csv' dbms=csv;
run;
Similarly in pandas, the opposite of ``read_csv`` is :meth:`~DataFrame.to_csv`,
and other data formats follow a similar api.
.. code-block:: python
tips.to_csv("tips2.csv")
Data operations
---------------
Operations on columns
~~~~~~~~~~~~~~~~~~~~~
In the ``DATA`` step, arbitrary math expressions can
be used on new or existing columns.
.. code-block:: sas
data tips;
set tips;
total_bill = total_bill - 2;
new_bill = total_bill / 2;
run;
.. include:: includes/column_operations.rst
Filtering
~~~~~~~~~
Filtering in SAS is done with an ``if`` or ``where`` statement, on one
or more columns.
.. code-block:: sas
data tips;
set tips;
if total_bill > 10;
run;
data tips;
set tips;
where total_bill > 10;
/* equivalent in this case - where happens before the
DATA step begins and can also be used in PROC statements */
run;
.. include:: includes/filtering.rst
If/then logic
~~~~~~~~~~~~~
In SAS, if/then logic can be used to create new columns.
.. code-block:: sas
data tips;
set tips;
format bucket $4.;
if total_bill < 10 then bucket = 'low';
else bucket = 'high';
run;
.. include:: includes/if_then.rst
Date functionality
~~~~~~~~~~~~~~~~~~
SAS provides a variety of functions to do operations on
date/datetime columns.
.. code-block:: sas
data tips;
set tips;
format date1 date2 date1_plusmonth mmddyy10.;
date1 = mdy(1, 15, 2013);
date2 = mdy(2, 15, 2015);
date1_year = year(date1);
date2_month = month(date2);
* shift date to beginning of next interval;
date1_next = intnx('MONTH', date1, 1);
* count intervals between dates;
months_between = intck('MONTH', date1, date2);
run;
The equivalent pandas operations are shown below. In addition to these
functions pandas supports other Time Series features
not available in Base SAS (such as resampling and custom offsets) -
see the :ref:`timeseries documentation<timeseries>` for more details.
.. include:: includes/time_date.rst
Selection of columns
~~~~~~~~~~~~~~~~~~~~
SAS provides keywords in the ``DATA`` step to select,
drop, and rename columns.
.. code-block:: sas
data tips;
set tips;
keep sex total_bill tip;
run;
data tips;
set tips;
drop sex;
run;
data tips;
set tips;
rename total_bill=total_bill_2;
run;
.. include:: includes/column_selection.rst
Sorting by values
~~~~~~~~~~~~~~~~~
Sorting in SAS is accomplished via ``PROC SORT``
.. code-block:: sas
proc sort data=tips;
by sex total_bill;
run;
.. include:: includes/sorting.rst
String processing
-----------------
Finding length of string
~~~~~~~~~~~~~~~~~~~~~~~~
SAS determines the length of a character string with the
`LENGTHN <https://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a002284668.htm>`__
and `LENGTHC <https://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a002283942.htm>`__
functions. ``LENGTHN`` excludes trailing blanks and ``LENGTHC`` includes trailing blanks.
.. code-block:: sas
data _null_;
set tips;
put(LENGTHN(time));
put(LENGTHC(time));
run;
.. include:: includes/length.rst
Finding position of substring
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SAS determines the position of a character in a string with the
`FINDW <https://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a002978282.htm>`__ function.
``FINDW`` takes the string defined by the first argument and searches for the first position of the substring
you supply as the second argument.
.. code-block:: sas
data _null_;
set tips;
put(FINDW(sex,'ale'));
run;
.. include:: includes/find_substring.rst
Extracting substring by position
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
SAS extracts a substring from a string based on its position with the
`SUBSTR <https://support.sas.com/documentation/cdl/en/imlug/66845/HTML/default/viewer.htm#imlug_langref_sect455.htm>`__ function.
.. code-block:: sas
data _null_;
set tips;
put(substr(sex,1,1));
run;
.. include:: includes/extract_substring.rst
Extracting nth word
~~~~~~~~~~~~~~~~~~~
The SAS `SCAN <https://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000214639.htm>`__
function returns the nth word from a string. The first argument is the string you want to parse and the
second argument specifies which word you want to extract.
.. code-block:: sas
data firstlast;
input String $60.;
First_Name = scan(string, 1);
Last_Name = scan(string, -1);
datalines2;
John Smith;
Jane Cook;
;;;
run;
.. include:: includes/nth_word.rst
Changing case
~~~~~~~~~~~~~
The SAS `UPCASE <https://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000245965.htm>`__
`LOWCASE <https://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000245912.htm>`__ and
`PROPCASE <https://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/a002598106.htm>`__
functions change the case of the argument.
.. code-block:: sas
data firstlast;
input String $60.;
string_up = UPCASE(string);
string_low = LOWCASE(string);
string_prop = PROPCASE(string);
datalines2;
John Smith;
Jane Cook;
;;;
run;
.. include:: includes/case.rst
Merging
-------
.. include:: includes/merge_setup.rst
In SAS, data must be explicitly sorted before merging. Different
types of joins are accomplished using the ``in=`` dummy
variables to track whether a match was found in one or both
input frames.
.. code-block:: sas
proc sort data=df1;
by key;
run;
proc sort data=df2;
by key;
run;
data left_join inner_join right_join outer_join;
merge df1(in=a) df2(in=b);
if a and b then output inner_join;
if a then output left_join;
if b then output right_join;
if a or b then output outer_join;
run;
.. include:: includes/merge.rst
Missing data
------------
Both pandas and SAS have a representation for missing data.
.. include:: includes/missing_intro.rst
One difference is that missing data cannot be compared to its sentinel value.
For example, in SAS you could do this to filter missing values.
.. code-block:: sas
data outer_join_nulls;
set outer_join;
if value_x = .;
run;
data outer_join_no_nulls;
set outer_join;
if value_x ^= .;
run;
.. include:: includes/missing.rst
GroupBy
-------
Aggregation
~~~~~~~~~~~
SAS's ``PROC SUMMARY`` can be used to group by one or
more key variables and compute aggregations on
numeric columns.
.. code-block:: sas
proc summary data=tips nway;
class sex smoker;
var total_bill tip;
output out=tips_summed sum=;
run;
.. include:: includes/groupby.rst
Transformation
~~~~~~~~~~~~~~
In SAS, if the group aggregations need to be used with
the original frame, it must be merged back together. For
example, to subtract the mean for each observation by smoker group.
.. code-block:: sas
proc summary data=tips missing nway;
class smoker;
var total_bill;
output out=smoker_means mean(total_bill)=group_bill;
run;
proc sort data=tips;
by smoker;
run;
data tips;
merge tips(in=a) smoker_means(in=b);
by smoker;
adj_total_bill = total_bill - group_bill;
if a and b;
run;
.. include:: includes/transform.rst
By group processing
~~~~~~~~~~~~~~~~~~~
In addition to aggregation, pandas ``groupby`` can be used to
replicate most other by group processing from SAS. For example,
this ``DATA`` step reads the data by sex/smoker group and filters to
the first entry for each.
.. code-block:: sas
proc sort data=tips;
by sex smoker;
run;
data tips_first;
set tips;
by sex smoker;
if FIRST.sex or FIRST.smoker then output;
run;
In pandas this would be written as:
.. ipython:: python
tips.groupby(["sex", "smoker"]).first()
Other considerations
--------------------
Disk vs memory
~~~~~~~~~~~~~~
pandas operates exclusively in memory, where a SAS data set exists on disk.
This means that the size of data able to be loaded in pandas is limited by your
machine's memory, but also that the operations on that data may be faster.
If out of core processing is needed, one possibility is the
`dask.dataframe <https://docs.dask.org/en/latest/dataframe.html>`_
library (currently in development) which
provides a subset of pandas functionality for an on-disk ``DataFrame``
Data interop
~~~~~~~~~~~~
pandas provides a :func:`read_sas` method that can read SAS data saved in
the XPORT or SAS7BDAT binary format.
.. code-block:: sas
libname xportout xport 'transport-file.xpt';
data xportout.tips;
set tips(rename=(total_bill=tbill));
* xport variable names limited to 6 characters;
run;
.. code-block:: python
df = pd.read_sas("transport-file.xpt")
df = pd.read_sas("binary-file.sas7bdat")
You can also specify the file format directly. By default, pandas will try
to infer the file format based on its extension.
.. code-block:: python
df = pd.read_sas("transport-file.xpt", format="xport")
df = pd.read_sas("binary-file.sas7bdat", format="sas7bdat")
XPORT is a relatively limited format and the parsing of it is not as
optimized as some of the other pandas readers. An alternative way
to interop data between SAS and pandas is to serialize to csv.
.. code-block:: ipython
# version 0.17, 10M rows
In [8]: %time df = pd.read_sas('big.xpt')
Wall time: 14.6 s
In [9]: %time df = pd.read_csv('big.csv')
Wall time: 4.86 s
|