Ejemplo n.º 1
0
from class_setup import print_with_title
from saspy import SASsession
sas = SASsession()

###############################################################################
#                                                                             #
# Exercise 10. [Python w/ saspy] Get info about a SAS session                 #
#                                                                             #
# Instructions: Uncomment the code immediately below, and then execute        #
#                                                                             #
###############################################################################

# Orginal Version
sas_submit_return_value = sas.submit('PROC PRODUCT_STATUS; RUN;')
sas_submit_log = sas_submit_return_value['LOG']
print_with_title(sas_submit_log, 'SAS log from PROC PRODUCT_STATUS:')

# Verify the output from PROC PRODUCT_STATUS is empty
print('The output from PROC PRODUCT_STATUS appears below.')
print(sas.submit('PROC PRODUCT_STATUS; RUN;')['LST'])
print('The output from PROC PRODUCT_STATUS appears above.')

# Compare the output of PROC PRODUCT_STATUS to PROC SETINIT
print(sas.submit('PROC SETINIT; RUN;')['LOG'])

# Refactor the sas.submit call to match Exercise 9
print(
    sas.submit('''
            PROC PRODUCT_STATUS;
            RUN;
        ''',
from class_setup import print_with_title
import platform




###############################################################################
#                                                                             #
# Exercise 0. [Python] Get version number                                     #
#                                                                             #
# Instructions: Uncomment the code immediately below, and then execute        #
#                                                                             #
###############################################################################


print_with_title(platform.sys.version, 'Python version being used:')


# Notes:
#
# 1. The following should be printed:
#    * Python version information
#    * Operating System information
#
# 2. To increase performance, only a small number of modules in Python's
#    standard library are available by default, so the platform module was
#    explicitly loaded at the start of this file.
#
# 3. This example illustrates three ways Python syntax differs from SAS:
#    * We don't need semicolons at the end of each statement. Unlike SAS,
#      semicolons are optional in Python, and they are typically only used to
Ejemplo n.º 3
0
###############################################################################
#                                                                             #
# Exercise 13. [Python w/ saspy] Imitate the SAS Macro Processor              #
#                                                                             #
# Instructions: Uncomment the code immediately below, and then execute        #
#                                                                             #
###############################################################################

# Original Version
sas_code_fragment = 'proc means data=sashelp.%s; run;'
for dsn in ['fish', 'iris']:
    sas_submit_return_value = sas.submit(sas_code_fragment % dsn,
                                         results='TEXT')
    print_with_title(
        sas_submit_return_value['LST'],
        'SAS results from PROC MEANS applies to sashelp.%s:' % dsn)

# Change the SAS procedure used
for dsn in ['fish', 'iris']:
    print(f'PROC PRINT applied to sashelp.{dsn}:')
    print(
        sas.submit(f'proc print data=sashelp.{dsn}(obs=5); run;',
                   results='TEXT')['LST'])

# Change the datasets used
for dsn in ['cars', 'class']:
    print(f'PROC MEAN applied to sashelp.{dsn}:')
    print(
        sas.submit(f'proc means data=sashelp.{dsn}; run;',
                   results='TEXT')['LST'])
Ejemplo n.º 4
0



###############################################################################
#                                                                             #
# Exercise 3. [Python] Define a list object                                   #
#                                                                             #
# Instructions: Uncomment the code immediately below, and then execute        #
#                                                                             #
###############################################################################


# Original Version
hello_world_list = ['Hello', 'list']
print_with_title(hello_world_list, 'The value of hello_world_list:')
print_with_title(type(hello_world_list), 'The type of hello_world_list:')

# Print out the initial element of the list
print(hello_world_list[0])

# Print out the final element of the list
print(hello_world_list[1])

# Create a list of length five, and print its middle elements
new_list = list(range(5))
print(new_list[2])


# Notes:
#
Ejemplo n.º 5
0
# 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
Ejemplo n.º 6
0
# 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 14. [Python w/ saspy] Adding and dropping columns                  #
#                                                                             #
# Instructions: Uncomment the code immediately below, and then execute        #
#                                                                             #
###############################################################################

# Original Version
class_df = sas.sasdata2dataframe(table='class', libref='sashelp')
print_with_title(class_df.head(), 'The first 5 rows of class_df:')

class_df['BMI'] = (class_df['Weight'] / class_df['Height']**2) * 703
print_with_title(
    class_df.head(),
    'The first 5 rows of class_df after a new column has been added:')

class_df.drop(columns=['Height', 'Weight'], inplace=True)
print_with_title(
    class_df.head(),
    'The first 5 rows of class_df after a two columns have been dropped:')

# Execute the SAS code equivalent
print('The results of the SAS code equivalent:')
print(
    sas.submit('''