IN SAS I need to call this python code: def modelcall(pathin,pathout,model, dataset,…
- User
IN SAS I need to call this python code: def model_call(path_in,path_out,model, dataset, debug):
codeimport xgboost import pandas as pd import math import category_encoders as ce if dataset.endswith(".sas7bdat") > 1: ds_path=path_in+dataset else : ds_path=path_in+dataset+".sas7bdat" model_path=path_in+model out_path=path_out+"outscore.csv" if debug ==1: print ("path: "+path_in) print ("model name: " +model) print ("dataset name: " +dataset) print("DS_path: " +ds_path) target_name='default12' time_name='period' intercept_name='Intercept' event_value='outstanding_bad' all_value='outstanding' id_vars=['aid'] df = pd.read_sas(ds_path, encoding='LATIN2') df[intercept_name]=1 df[event_value]=df['app_loan_amount']*df[target_name] df[all_value]=df['app_loan_amount'] #List of variables vars = [var for var in list(df) if var[0:3].lower() in ['app','act']] # vars = [var for var in list(df) if var[0:3].lower() in ['app','act','agr','ags']] #Splitting into numeric and character variables varsc = list(df[vars].select_dtypes(include='object')) varsn = list(df[vars].select_dtypes(include='number')) #Categorical variables coding enc = ce.BinaryEncoder(cols=varsc) df_ce = enc.fit_transform(df[varsc]) varsc_ce = list(df_ce)df_ce = enc.fit_transform(df)
codedf_ce=df vars_ce = varsnvars_ce = varsn + varsc_ce
code - Assistant
In SAS, you can use PROC PYTHON to execute Python code within a SAS environment. You can pass the necessary arguments to the Python function using the provided SAS macro variables. Here's how you can call the
model_callfunction with the given arguments:sas%let scoring_dir=your_scoring_directory; %let model = xgb1.model; %let dataset = indata; %let debug_flag = 0; proc python; submit; def model_call(path_in,path_out,model, dataset, debug): import xgboost import pandas as pd import math import category_encoders as ce # ... rest of the code ... return 0 # Call the function with SAS macro variables model_call("&scoring_dir", "&scoring_dir", "&model", "&dataset", &debug_flag) endsubmit; run; - User
there is no such proc as proc python.
- Assistant
I apologize for the confusion. It seems I misunderstood the available capabilities in SAS for calling Python code directly. Unfortunately, SAS does not have a built-in method for executing Python code like I mentioned earlier.
However, you can use the
%SYSEXECstatement in SAS to call Python code from an external file. Here's how to do that:- Save the Python code into an external Python file, e.g.,
model_call.py. Make sure that the function call is included at the end of the script:
python# model_call.py def model_call(path_in, path_out, model, dataset, debug): # ... rest of the code ... model_call("&scoring_dir", "&scoring_dir", "&model", "&dataset", &debug_flag)- In your SAS code, call the external Python script file using the statement:
- Save the Python code into an external Python file, e.g.,