Skip to content
TheChatArchiveThe Chat Archive
gpt-4-0314· openaiHugging Face Suzhen/CodeChat

IN SAS I need to call this python code: def modelcall(pathin,pathout,model, dataset,…

  1. User

    IN SAS I need to call this python code: def model_call(path_in,path_out,model, dataset, debug):

    import 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)

    df_ce=df
    
    vars_ce = varsn
    

    vars_ce = varsn + varsc_ce

    test = df_ce
    test[target_name]=1
    
    X_test=test[vars_ce]
    Y_test=test[target_name]
    
    
    if debug ==1:    
        print (test.shape)
        print (df.shape)
    
    xdm_test  = xgboost.DMatrix(X_test, Y_test, enable_categorical=True, missing=True)
    
        
    model = xgboost.Booster()
    
    model.load_model(model_path)
    
    Y_pred_test = model.predict(xdm_test)
    
    df_out=df
    
    df_out['SCORECARD_POINTS']= pd.DataFrame(Y_pred_test)
    
    fin_vars= ['SCORECARD_POINTS'] + [time_name] + id_vars
    
    df_out=df_out[fin_vars]
    
    df_out.to_csv(out_path, index=False)
    
        
    return 0
    

    with aruments : ("model_call", "&scoring_dir", "&scoring_dir", 'xgb1.model', "indata", 0)

  2. 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_call function with the given arguments:

    %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;
    

    Make sure to replace your_scoring_directory with the actual path to the scoring directory. You can also adjust the values of other macro variables as needed. Note that we wrapped the Python code inside a submit and endsubmit block to execute it within the PROC PYTHON step.

  3. User

    there is no such proc as proc python.

  4. 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 %SYSEXEC statement in SAS to call Python code from an external file. Here's how to do that:

    1. 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:
    # 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)
    
    1. In your SAS code, call the external Python script file using the %SYSEXEC statement:
    %let scoring_dir=your_scoring_directory;
    %let model = xgb1.model;
    %let dataset = indata;
    %let debug_flag = 0;
    
    %let py_script = model_call.py;
    
    %sysexec python "&py_script" --scoring_dir &scoring_dir --model &model --dataset &dataset --debug_flag &debug_flag;
    

    This will call the external Python script and pass the necessary arguments through the command line.

    Please note that the %SYSEXEC statement is available only on UNIX and Windows operating systems. If you are using SAS on other platforms, you may need to use other SAS methods or tools to call external scripts.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.