// Python in SAS Viya

Corresponding codes 

/*========================================================================== 

*  PROC PYTHON MINIMAL EXAMPLE 

 *==========================================================================*/ 

PROC PYTHON; 

submit; 

print("Hello World!") 

endsubmit; 

QUIT; 

/*========================================================================== 

 *  PROC PYTHON USING PACKAGES 

 *==========================================================================*/ 

PROC PYTHON; 

submit; 

import pandas as pd 

df = pd.DataFrame({ 

    "Product": ["A", "B", "C"], 

    "Sales": [120, 250, 180] 

}) 

print(df) 

endsubmit; 

QUIT; 

/*========================================================================== 

*  MANAGING PYTHON PACKAGES 

 *==========================================================================*/ 

%LET env_name = projectx; 

OPTIONS SET=PROC_M2PATH='/opt/sas/viya/home/SASFoundation/misc/tk'; 

OPTIONS SET=PROC_PYPATH="/sasdata/python/&env_name./bin/python3.11"; 

OPTIONS SET=LD_LIBRARY_PATH="&LD_LIB.:/sasdata/python/&env_name./lib/:/sasdata/python/&env_name./lib64/:/usr/lib64:/opt/python/Python3.11/lib"; 

PROC PYTHON restart; 

submit; 

import os 

import sys 

import subprocess 

env_name=SAS.symget("env_name") 

subprocess.check_call([sys.executable, '-m', 'pip', 'install', '--upgrade', 'pip']) 

subprocess.check_call([sys.executable, '-m', 'pip', 'install', 'setuptools']) 

subprocess.check_call([sys.executable, '-m', 'pip', 'install', '-r' f'/sasdata/pythonpackages/{env_name}-requirements.txt']) 

with open(f'/sasdata/pythonpackages/{env_name}-requirements.txt', "w") as new_requirements: 

    subprocess.call([sys.executable, '-m', "pip", "freeze"], stdout=new_requirements) 

endsubmit; 

QUIT; 

/*========================================================================== 

*  SWAT - CONNECT TO CAS WITH USER/PASS 

 *==========================================================================*/ 

import swat 

conn = swat.CAS( 

    host="viya.company.com",  

    port=443, 

    username="your_username", 

    password="your_password" 

/*========================================================================== 

*  SWAT - CONNECT TO CAS WITH OAUTH TOKEN 

 *==========================================================================*/ 

import os 

import swat 

os.environ['CAS_CLIENT_SSL_CA_LIST'] = r"path/to/trustedcerts.pem" 

oauth_token = 'insert_your_oauth_token_here' 

conn = swat.CAS( 

    "viya.company.com",  

    port=443,  

    protocol="https", 

    path="/cas-shared-default-http", 

    password=oauth_token 

 /*========================================================================== 

*  SWAT - INTERACTING WITH CAS 

 *==========================================================================*/ 

# Inspect available tables in the PUBLIC caslib 

conn.table.tableInfo(caslib="PUBLIC") 

# Load the target table from the PUBLIC caslib 

cars = conn.CASTable( 

    "CARS", 

    caslib="PUBLIC" 

# Display the first few rows of the CARS table 

cars.head() 

# Display summary statistics for the CARS table 

cars.summary() 

 /*========================================================================== 

*  SWAT - MOVING DATA FROM PANDAS TO CAS 

 *==========================================================================*/ 

import pandas as pd 

df = pd.DataFrame({ 

    "Region": ["North", "South"], 

    "Revenue": [125000, 143000] 

}) 

conn.upload_frame( 

    df, 

    casout={ 

        "name": "REVENUE", 

        "caslib": "PUBLIC", 

        "promote": True 

    } 

 /*========================================================================== 

*  SWAT - RETRIEVING DATA FROM CAS 

 *==========================================================================*/ 

revenue = conn.CASTable("REVENUE") 

pdf = revenue.to_frame() 

print(pdf)