-
Notifications
You must be signed in to change notification settings - Fork 2
/
Exercise_File_E-SASPy_Convenience_Methods.py
109 lines (90 loc) · 4.65 KB
/
Exercise_File_E-SASPy_Convenience_Methods.py
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
# Everything is better with friends: Executing SAS® code in Python scripts with
# SASPy, and turbocharging your SAS programming with open-source tooling
#
# Half-day class, Western Users of SAS Software (WUSS) 2019
###############################################################################
# Exercises 11-12: SASPy Convenience Methods #
###############################################################################
# Lines 12-14 load modules needed for exercises and should be left as-is
from class_setup import print_with_title
from saspy import SASsession
sas = SASsession()
###############################################################################
# #
# Exercise 11. [Python w/ saspy] Connect directly to a SAS dataset #
# #
# Instructions: Uncomment the code immediately below, and then execute #
# #
###############################################################################
# fish_sds = sas.sasdata(table='fish', libref='sashelp')
# print_with_title(type(fish_sds), 'The type of fish_sds:')
# print_with_title(
# fish_sds.columnInfo(),
# 'The Python equivalent of PROC CONTENTS:'
# )
# print_with_title(fish_sds.means(), 'The Python equivalent of PROC MEANS:')
# Notes:
# 1. The SASdata object fish_sds (meaning a direct connection to the disk-based
# SAS dataset sashelp.fish, not an in-memory DataFrame) is created, and the
# following are printed:
# * the type of object fish_sds
# * the column-information portion of PROC CONTENTS applied to the SAS
# dataset sashelp.fish
# * summary information about the 7 columns in sashelp.fish
#
# 2. The sas object, which was created at the beginning of this file, is a
# persistent connection to a SAS session, and its sasdata method is used to
# create the connection to sashelp.fish.
#
# 3. The fish_sds object calls its convenience method means, which implicitly
# invokes PROC MEANS on sashelp.fish.
#
# 4. All subsequent exercises in this file will assume the object fish_sds
# exists, so please don't comment out the line creating it.
#
# 5. For additional practice, try out the additional convenience methods at
# https://sassoftware.github.io/saspy/api.html#sas-data-object.
###############################################################################
# #
# Exercise 12. [Python w/ saspy] Get convenience method SAS code #
# #
# Instructions: Uncomment the code immediately below, and then execute #
# #
###############################################################################
# sas.teach_me_SAS(True)
# print_with_title(
# '',
# 'The SAS code generated by fish_sds.means():',
# linebreaks_after=0,
# put_stars_after=False
# )
# fish_sds.means()
# print_with_title('', '', linebreaks_before=0, linebreaks_after=0)
# sas.teach_me_SAS(False)
# Notes:
# 1. The SASdata object fish_sds, which was created in an exercise above as a
# direct connection to the SAS dataset sashelp.fish, calls its convenience
# method means within a "Teach Me SAS" sandwich, and the following is
# printed:
# * the SAS code for the PROC MEANS step implicitly generated by the means
# convenience method
#
# 2. The sas object, which was created at the beginning of this file, is a
# persistent connection to a SAS session, and its teach_me_SAS method is
# used as follows:
# * When called with argument True, SAS output is suppressed for all
# subsequent saspy convenience methods, and the SAS code generated is
# returned instead.
# * When teach_me_SAS is called with argument False, this behavior is turned
# off.
# 3. True and False are standard Python objects. Like their SAS equivalents,
# they are interchangeable with the values 1 and 0, respectively.
#
# 4. One benefit of this process is being able to extract and modify the SAS
# code. For example, if a convenience method doesn't offer an option like a
# class statement for PROC MEANS, we can manually add it to the code
# generated by the teach_me_SAS method and then execute the modified SAS
# code using the submit method.
#
# 5. For additional practice, try getting the SAS code for other methods at
# https://sassoftware.github.io/saspy/api.html#sas-data-object.