/*******************************************************************************/
/*  Copyright© 2025, SAS Institute Inc., Cary, NC, USA.  All Rights Reserved.  */
/*  SPDX-License-Identifier: Apache-2.0                                        */
/*******************     File history and update details     *******************/
/*  Earliest know year for publication of the import_homeequity.sas file is    */
/*  2022 as part of the SAS Viya Quick Start files.                            */
/*  04JAN2026 - Jos van der Velden                                             */
/*            - proc casutil was adapted to assure an existing table with the  */
/*              name HOMEEQUITY would be removed before loading and saving.    */
/*            - The propcase function was added to standardize CITY.           */
/*            - LTV was added as a calculated variable.                        */
/*            - The filename was renamed to "Import_and_load_HOMEEQUITY.sas".  */
/*  29JUL2026 - Jos van der Velden                                             */
/*            - explanations for all statements and steps were included.       */
/*            - the extension ".txt" was added to the filename to avoid        */
/*              confusion with the .sas file without explanations.             */
/*******************************************************************************/

/*******************************************************************************/
/* Read the home_equity.csv sample data from the SAS Support website using     */
/* the http procedure. Import that csv file as work.t_homeequity.              */
/* Provide labels for the variables, change the casing for the variable city,  */
/* add the variable LTV and save the resulting dataset as work.homeequity.     */
/* Then load the resulting dataset work.homeequity into the casuser library    */
/* as an in-memory table so that it can be used in visual analytics.           */
/* Note: short variable names were provided on the first line of the csv-file  */ 
/*******************************************************************************/

/*******************************************************************************/
/* NOTE: Kaggle has the hmeq dataset with 13 variables listed as a datasource. */
/* The link is: https://www.kaggle.com/datasets/ajay1735/hmeq-data.            */
/* The variables are: BAD,LOAN,MORTDUE,VALUE,REASON,JOB,YOJ,                   */
/*                    DEROG,DELINQ,CLAGE,NINQ,CLNO,DEBTINC.                    */
/*******************************************************************************/

/*******************************************************************************/
/* This SAS statement creates a temporary storage location.                    */
/* Each SAS statement end with a semicolon (;).                                */
/* SAS will use it to store the CSV file that is downloaded from the internet. */
/*   * The filename statement creates a file reference.                        */
/*   * DATA is the name of the file reference.                                 */
/*   * TEMP means the file is temporary.                                       */
/*   * The file will exist only during the SAS session.                        */
/*******************************************************************************/
filename data TEMP;

/*******************************************************************************/
/* This procedure step downloads the CSV file from a web URL and saves it      */
/* in the temporary file reference called data.                                */
/*   * PROC HTTP starts the HTTP procedure. This procedure can request files   */
/*     or information from the internet.                                       */
/*   * GET means: retrieve information from a web address.                     */
/*     Here, SAS asks the website to send the CSV file.                        */
/*   * URL specifies the web address of the CSV file.                          */
/*   * OUT = DATA saves the downloaded CSV file into the temporary file        */
/*     reference named data.                                                   */
/*   * RUN terminates the procedure step.                                      */
/*******************************************************************************/
proc http 
   method = "GET" 
   url    = "https://support.sas.com/documentation/onlinedoc/viya/exampledatasets/home_equity.csv" 
   out    = data;
run;

/*******************************************************************************/
/* This procedure step reads the downloaded CSV file and creates a             */
/* SAS data set called work.t_homeequity. This is a temporary working          */
/* version of the data.                                                        */
/*   * PROC IMPORT imports external data into SAS.                             */
/*   * FILE = "DATA" tells SAS to read the temporary file reference            */
/*     named data.                                                             */
/*   * DBMS = CSV tells SAS that the file type is CSV.                         */
/*   * OUT = WORK.T_HOMEEQUITY creates a SAS table named t_homeequity          */
/*     in the WORK library.                                                    */
/*   * REPLACE means: if a table with this name already exists, overwrite it.  */
/*   * GUESSINGROWS = 5960 specifies the amount of rows in the CSV file SAS    */
/*     will inspect to decide the variable type and length whem importing.     */
/*   * RUN terminates the procedure step.                                      */
/*******************************************************************************/
proc import file="data" dbms=csv out=work.t_homeequity replace;
    guessingrows=5960;
run;

/*******************************************************************************/
/* This data step creates a new SAS table called WORK.HOMEEQUITY. It reads     */
/* the temporary table, adds labels, applies formats, changes the appearance   */
/* of city names, and creates a new variable called LTV.                       */
/*   * DATA is the keyword data starts the data step.                          */
/*     A new SAS data set called HOMEEQUITY is created in the WORK library.    */
/*   * SET WORK.T_HOMEEQUITY reads all rows and columns from the imported      */
/*     table work.t_homeequity.                                                */
/*   * The new table is based on this input data.                              */
/*   * The label statement gives variables longer, clearer descriptions.       */
/*       - the label for LOAN is set to "Amount of Loan Request".              */
/*       - the label for MORTDUE is set to "Amount Due on Existing Mortgage".  */
/*       - the label for LTV is set to "Loan to Value ratio".                  */
/*       - etc                                                                 */
/*       Important: a label does not rename the variable. It only gives        */
/*       the variable a clearer description for reports and metadata.          */
/*   * The format statement controls how values are displayed.                 */
/*     It does not change the value stored in the dataset.                     */
/*     It changes how SAS shows the value. Some examples:                      */
/*       - APPDATE date9. displays the application date in a date style        */
/*                        such as 01JAN2025. In the dataset it is stored       */
/*                        as a number. In SAS 01JAN1960 is stored as 0.        */
/*                        02JAN1960 is stored as 1. Etc.                       */
/*       - CLAGE comma8.1 displays the age of the oldest credit line with      */
/*                        commas and one decimal place.                        */
/*       - LOAN MORTDUE VALUE dollar12. displays these money variables         */
/*                                      as dollar amounts.                     */
/*       - etc.                                                                */
/*   * The statement "INFORMAT _ALL_;" clears or resets informats for all      */
/*     variables in the output data set. This is often used after importing    */
/*     data, because PROC IMPORT may assign informats automatically.           */
/*   * The statement "CITY = propcase(CITY);" uses the propcase function to    */
/*     changes the value of CITY to proper case. For example:                  */
/*        - "NEW YORK" is changed to "New York"                                */
/*        - "los angeles" is changed to "Los Angeles"                          */
/*   * The statement "LTV = MORTDUE / VALUE;" creates a new variable           */
/*     called LTV. It is calculated as "Amount due on existing mortgage"       */
/*     divided by "Value of current property". It is diplayed as a percentage  */
/*     because the program applies the percent10.2 format. Example: 80%.       */
/*   * RUN terminates the procedure step.                                      */
/*******************************************************************************/
data work.homeequity;
    set work.t_homeequity;
    label APPDATE  = "Loan Application Date"
          BAD      = "Loan Status"
          CITY     = "City"
          CLAGE    = "Age of Oldest Credit Line (months)"
          CLNO     = "Number of Credit Lines"
          DEBTINC  = "Debt to Income Ratio"
          DELINQ   = "Number of Delinquent Credit Lines"
          DEROG    = "Number of Derogatory Reports"
          DIVISION = "Division"
          JOB      = "Job Category"
          LOAN     = "Amount of Loan Request"
          MORTDUE  = "Amount Due on Existing Mortgage"
          NINQ     = "Number of Recent Credit Inquiries"
          REASON   = "Loan Purpose"
          REGION   = "Region"
          STATE    = "State"
          VALUE    = "Value of Current Property"
          YOJ      = "Years at Present Job"
          LTV      = "Loan to Value ratio";
    format APPDATE   date9.
           CLAGE     comma8.1
           LOAN MORTDUE VALUE dollar12.
           DEBTINC   8.1
           LTV       percent10.2
           BAD CITY CLNO DELINQ DEROG DIVISION JOB NINQ REASON REGION STATE YOJ;
    informat _all_;
    CITY  = propcase(CITY);
    LTV   = MORTDUE / VALUE;
run;

/*******************************************************************************/
/* This procedure step deletes the temporary table WORK.T_HOMEEQUITY. It is no */
/* longer needed because the cleaned table WORK.HOMEEQUITY has been created.   */
/*   * PROC DATASETS starts the DATASETS procedure.                            */
/*   * LIB = WORK means it works with the WORK library.                        */
/*   * NOLIST means SAS should not display a full list of all data sets        */
/*     in the library.                                                         */
/*   * The statement "DELETE T_HOMEEQUITY;" sppecifies the table to delete.    */
/*   * RUN terminates the procedure step.                                      */
/*******************************************************************************/
proc datasets lib = work nolist;
    delete t_homeequity;
run;

/*******************************************************************************/
/* This procedure step shows metadata about the final table work.homeequity.   */
/* Metadata means information about the data, such as variable names, labels,  */
/* types, formats, and order.                                                  */
/*   * PROC CONTENTS starts the CONTENTS procedure.                            */
/*   * DATA = WORK.HOMEEQUITY tells SAS which table to describe.               */
/*   * VARNUM shows variables in their original order in the data set.         */
/*   * RUN terminates the procedure step.                                      */
/*******************************************************************************/
proc contents data = work.homeequity varnum;
run;

/*******************************************************************************/
/* This statement removes the temporary file reference called data.            */
/*   * FILENAME manages file references.                                       */
/*   * DATA is the file reference created earlier.                             */
/*   * CLEAR removes the file reference.                                       */
/*******************************************************************************/
filename data clear;

/*******************************************************************************/
/* This starts a default CAS session. CAS stands for Cloud Analytic Services.  */
/* CAS is the in-memory analytics engine used by SAS Viya. The data will later */
/* be loaded into CAS so it can be used in tools such as SAS Visual Analytics. */
/*******************************************************************************/
cas; 

/*******************************************************************************/
/* This statement creates a SAS library reference called CASUSER.              */
/* It points to the CAS caslib also called CASUSER.                            */
/*   * LIBNAME creates a library reference.                                    */
/*   * CASUSER is the library name that appears in SAS.                        */
/*   * CAS tells SAS this library connects to CAS.                             */
/*   * CASLIB = "CASUSER" points to the CASUSER caslib in CAS.                 */
/*******************************************************************************/
libname CASUSER cas caslib = "CASUSER";

/*******************************************************************************/
/* This procedure step loads the final SAS table work.homeequity into CAS as   */
/* an in-memory table called HOMEEQUITY. It also saves it as a .sashdat file   */
/* and promotes the table so it can be used more broadly in the CAS session    */
/* or by other tools.                                                          */
/*   * PROC CASUTIL starts the CASUTIL procedure. This procedure is used to    */
/*     manage CAS tables. It can load, save, drop, and promote tables in CAS.  */
/*   * droptable deletes a CAS table.                                          */
/*     - CASDATA = "HOMEEQUITY" is the CAS table to delete.                    */
/*     - INCASLIB = "CASUSER" tells SAS where to look for the table.           */
/*     - QUIET means: do not show an error if the table does not exist.        */
/*     This prevents problems if a table called HOMEEQUITY already exists.     */
/*   * LOAD loads a SAS data set into CAS.                                     */
/*     - DATA = WORK.HOMEEQUITY is the source table in the SAS WORK library.   */
/*     - CASOUT = "HOMEEQUITY" is the name of the new CAS table.               */
/*     - OUTCASLIB = "CASUSER" tells SAS to put the CAS table in the           */
/*       CASUSER caslib.                                                       */
/*     - REPLACE means overwrite the CAS table if it already exists.           */
/*     After this, the table is available in memory in CAS.                    */
/*   * SAVE saves the in-memory CAS table to disk.                             */
/*     - CASDATA = "HOMEEQUITY" is the table currently in memory.              */
/*     - INCASLIB = "CASUSER" tells SAS where the table is located.            */
/*     - OUTCASLIB = "CASUSER" tells SAS where to save the file.               */
/*     - CASOUT = "HOMEEQUITY.SASHDAT" is the file name.                       */
/*     - REPLACE overwrites the saved file if it already exists.               */
/*     A .sashdat file is a SAS Viya data file format used by CAS.             */
/*   * PROMOTE makes the CAS table more widely available. Without promotion,   */
/*     the CAS table may only be available in the current session. After       */
/*     promotion, other tools or users with permission may be able to use it.  */
/*     This is important when the table must be used in SAS Visual Analytics.  */
/*   * RUN terminates the procedure step.                                      */
/*******************************************************************************/
proc casutil;
   droptable casdata = "HOMEEQUITY" incaslib = "CASUSER" quiet;

   load data      = work.homeequity
        casout    = "HOMEEQUITY"
        outcaslib = "CASUSER"
        replace;

   save casdata   = "HOMEEQUITY"
        incaslib  = "CASUSER"
        outcaslib = "CASUSER"
        casout    = "HOMEEQUITY.sashdat"
        replace;

   promote casdata   = "HOMEEQUITY"
           incaslib  = "CASUSER"
           outcaslib = "CASUSER";
run;