0% found this document useful (0 votes)
149 views

SAS Project Solution Snapshots

This document contains SAS code that validates user-provided inputs, checks for errors, and calls macros to generate summary statistics from input data. Specifically, it checks that required input locations and dataset names are provided, verifies datasets exist, and checks for required variables. If no errors, it calls macros to pull customer data between specified dates and create characteristic variables with lags.

Uploaded by

naman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
149 views

SAS Project Solution Snapshots

This document contains SAS code that validates user-provided inputs, checks for errors, and calls macros to generate summary statistics from input data. Specifically, it checks that required input locations and dataset names are provided, verifies datasets exist, and checks for required variables. If no errors, it calls macros to pull customer data between specified dates and create characteristic variables with lags.

Uploaded by

naman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 7

/*xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx Start of User Inputs xxxxxxxxxxxxxxx

xxxxxxxxxxxxxxxxxxxxxxxxxx/
/* Specify the location of the input dataset */
%let DataLoc = C:\Personal\ANZ\Case study;
/* Specify the location to store the logs/reports */
%let OutputLoc=C:\Personal\ANZ\Case study;
/* Specify the application dataset variable name.This dataset should have below
variables.*/
%let varlist=n_cust;
%let charlist=A_BAL_CUR A_CADV K_DAY_DLNQT_CUR;
%let mean_variables = A_BAL_CUR A_CADV;
%let max_variables=k_day_DLNQT_CUR a_cadv;
%let min_variables=A_BAL_CUR ;
/* Specify the input dataset names for application and performance. */
%let AppData=sample_cust;
/* Specify the name for the output dataset. The output dataset will be stored un
der DataLoc */
%let OutData=a;
/* Specify Application start and end date.Date should be in date9. format(DDMONY
YYY)*/
%let start_mon=01jan2007;
%let end_mon=01feb2007;
/*xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx End of User Inputs xxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxx/
/** Do not alter below code **/
%let CodeLoc=C:\Personal\ANZ\Case study;
%include "&CodeLoc\includes\dm_cra_check_user_inputs.sas";

/*
/ Program : dm_check_user_inputs.sas
/ Version : v1.0
/ Author : Shivkumar
/ Created : 30-SEP-2015
/ LastModBy :
/ LastModDt :
/ Purpose : To create the libraries, folder structure,validate the user inpu
t, perform error handling,
print the log contents to a file and call the pr
ocess macros for generation of summary counts
/===============================================================================
=============
/ AMENDMENT HISTORY:
/ ------------------------------------------------------------------------------
-------
/ init Date Decription
/ ------------------------------------------------------------------------------
-------
/ PS 30-SEP-2015 Added code to display user passed parameters from EG interface
to log
/ PS 30-SEP-2015 Removing temp directory creation & library, maxlog parameter
/===============================================================================
=============
*/
options mreplace symbolgen;
/*proc catalog c=work.sasmacr kill force;*/
/*quit;*/
/*xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxx*/
/** Do not alter below code **/
%macro dm_cra;
/* Setting the MPRINT SAS system option to print the resolved SAS macro code in
the log */
options mprint symbolgen mlogic;
/* Include autoexec to make macros available */
%include "&CodeLoc\includes\dm_autoexec.sas";
/* Some global macro variables */
%let LogSaved=0; * to save log;
%let ErrCode=0; * To handle errors;

/* Check on Dataloc, if user entered a valid input */


%if not %LENGTH(&DataLoc) > 0 %then %do;
%put ERROR: Mandatory Variable "DataLoc" is empty, Please enter a valid data lo
cation;
%let ErrCode=1;
%goto Exit_DM_CRA;
%end;
/* Check if data location exists */
%PUT check=%sysfunc(fileexist(&DataLoc));
%if not %sysfunc(fileexist(&DataLoc)) %then %do;
%put ERROR: The path specified for data does not exist;
%put ERROR: &DataLoc;
%let ErrCode=1;
%goto Exit_DM_CRA;
%end;
/* Check on OutputLoc, if user entered a valid input */
%if not %LENGTH(&OutputLoc) > 0 %then %do;
%put ERROR: Mandatory Variable "OutputLoc" is empty, Please enter a valid data
location;
%let ErrCode=1;
%goto Exit_DM_CRA;
%end;
/* Check if data location exists */
%PUT check=%sysfunc(fileexist(&OutputLoc));
%if not %sysfunc(fileexist(&OutputLoc)) %then %do;
%put ERROR: The path specified for OutputLoc does not exist;
%put ERROR: &OutputLoc;
%let ErrCode=1;
%end;
/* Halt process if error code is 1 */
%if &ErrCode=1 %then %goto Exit_DM_CRA;
/* Remove the last slash if included */
%let DataLoc = %removelastslash(&DataLoc);
%let OutputLoc=%removelastslash(&OutputLoc); /* path of output files, logs, temp
& work Added 10/06/2015 */
%let DataPath= &DataLoc;
%let LogsPath=&OutputLoc\logs;
%let OutputPath=&OutputLoc\output;
%let WorkFolder=&OutputLoc\work;
/*%let WorkPath=&WorkFolder/temp_&SYSJOBID;*/
%let WorkPath=&WorkFolder\temp;
/* Create folder structure */
%create_directory(dir=&OutputPath);
%create_directory(dir=&LogsPath);
%create_directory(dir=&WorkFolder);
%create_directory(dir=&WorkPath);
/*** Check if directories has been created, if not exit ****/
/* Assign Libraries */
Libname InLib "&DataPath";
Libname DMWORK "&WorkPath";
/* User passed parameters to log */
%put ***************************************************************************
******;
%put USER PASSED MACRO PARAMETERS:;
%put ***************************************************************************
******;
%put DataLoc=&DataLoc;
%put OutputLoc=&OutputLoc;
%put OutputLoc= &OutputLoc;
%put ***************************************************************************
******;
/* Save log */
%let stime = %sysfunc(putn(%sysfunc(datetime()), datetime16.));
%let LogName=&sysuserid._case study;
%let ProgName=01-case study;
%savelog(Logpath=&LogsPath, Logname=&LogName,WorkLib=DMWORK);
%let LogSaved=1;
/* Check if user passes a valid output dataset name */
%if not %LENGTH(&OutData) > 0 %then %do;
%put ERROR: Output dataset name is blank, please specify a valid SAS dataset na
me;
%let ErrCode=1;
%end;
%else %if %LENGTH(&OutData) > 32 %then %do;
%put ERROR: The length of Output dataset name exceeds 32 characters, please spe
cify a valid SAS dataset name within maximum length 30;
%let ErrCode=1;
%end;
/* Check if input dataset exist */
/* Check if input dataset that user passed exists */
%if not %DataSets_Exist(inlib.&AppData) %then %do;
%put ERROR: Input dataset "&AppData" does not exist in location below:;
%put ERROR: &DataLoc;
%let ErrCode=1;
%goto Exit_DM_CRA;
%end;
/* Check if required columns exists */
%if not %hasvars(inlib.&AppData, &varlist) %then %do;
%put ERROR: Below Merge variables do not exist in &AppData;
%put ERROR: &_nomatch_;
%let ErrCode=1;
%end;
%if &ErrCode=1 %then %goto Exit_DM_CRA;
%let WorkLib=DMWORK;
/* Get min and max date from input dataset */
/*proc sql noprint;*/
/*select max(app_date),min(app_date) into :maxapp_date ,:minapp_date from inlib.
&AppData;*/
/*quit;*/

/*%let start_month=%sysfunc(putn(&minapp_date,date9.)); */
/*%let end_month=%sysfunc(putn(&maxapp_date,date9.)); */
/*%put &maxapp_date &minapp_date &start_month &end_month; */
%include "&CodeLoc.\includes\dm_CRA_rating_chargen.sas";
*%pull_cust_data(startdate=01jan2009,enddate=31mar2009);
/*%xholding_datapull(Start_mon=&start_mon, end_mon=&end_mon, WorkLib=DMWORK);*/
/*%Create_chars(In_dsn=all_chars,Out_dsn=all_chars_final ,list=&charlist,opp=,n=
12);*/
/* EXIT Code: */
%Exit_DM_CRA:
%if &LogSaved %then %do;
proc printto;
run;
%getlog(Logpath=&LogsPath,
Logname=&LogName, Program=&ProgName, starttime=&stime, WorkLib=
DMWORK);
%end;
/* Halt the process, if there is an error */
%if &ErrCode = 1 %then %do;
%put ERROR: Program halted!;
%end;
%mend;
/* Invoke the Macro */
%dm_cra
%macro Pull_Cust_data(startdate=,enddate=);
data _null_;
call symput("startcustdate",compress("'"||put("&startdate"d, YYMMDD10.)||"
'"));
call symput("endcustdate",compress("'"||put("&enddate"d, YYMMDD10.)||"'"))
;
run;
%mend;
data dmwork.all_chars;
infile datalines dlm='09'x dsd missover;
input n_acct d_monthly_label:monyy5. A_BAL_CUR A_CADV K_DAY_DLNQT_CUR;
format d_monthly_label monyy7.;
datalines;
1 Apr14 3386 170 34
1 May14 3428 318 24
1 Jun14 3720 318 3
1 Jul14 1737 225 27
1 Aug14 2741 373 3
1 Sep14 2605 295 12
1 Oct14 8126 225 4
1 Nov14 6918 346 9
2 Jan14 5743 382 25
2 Feb14 9077 442 10
2 Mar14 8236 209 25
2 Apr14 5321 287 23
2 May14 9138 303 12
2 Jun14 9882 171 23
2 Jul14 4829 386 1
2 Aug14 9639 361 14
2 Sep14 8596 142 24
2 Oct14 6629 186 8
2 Nov14 7947 466 3
2 Dec14 2808 360 34
3 Jan14 3546 451 14
3 Feb14 4259 483 3
3 Mar14 3302 301 32
3 Apr14 2496 447 5
3 May14 1132 489 6
3 Jun14 6606 226 14
4 Jul14 1436 330 30
4 Aug14 1443 324 15
4 Sep14 5475 211 29
4 Oct14 4709 133 20
4 Nov14 5777 111 6
4 Dec14 8451 157 5
;
run;
%macro Create_chars(In_dsn=, /*Input data set name */
Out_dsn=, /*Output data set name */
list=, /*Variable on which to compute the average */
opp=, /* Variable for moving average */
n= /* Number of observations period on which to comp
ute the average */);
data dmwork.&Out_dsn;
set dmwork.&In_dsn;
by n_acct;
drop _: count k &list;
%do z=1 %to %words(&list.);
%let usr_char=%scan(&list.,&z);
/* keep A_BAL_CUR N_ACCT a_cadv &usr_car._1-&usr_car._12 &usr_car._L1
&usr_car._L3 &usr_car._L6 &usr_car._L9 &usr_car._L12;*/
*compute the lags;
_&usr_char._1= &usr_char;
%do i = 1 %to &n - 1;
%let Num = %eval(&i + 1);
_&usr_char._&Num = lag&i(&usr_char.);
%end;
&usr_char._1=&usr_char;
array &usr_char.arr(*) &usr_char._2-&usr_char._12;
%do j = 2 %to 12;
%let Num = %eval(&j );
&usr_char._&Num = lag%eval(&j-1)(&usr_char.);
%end;
/*Assigning missing values for non lagged values*/
if first.n_acct then count=1;
do k=count to dim(&usr_char.arr);
&usr_char.arr(k)=.;
end;
%end;
count + 1;
if last.n_acct then output;
run;
data dmwork.&Out_dsn;
set dmwork.&Out_dsn;
%do aa=1 %to %words(&mean_variables.);
%let usr_char=%scan(&mean_variables.,&aa);
%let opp=mean;
&opp._&usr_char._L1=&usr_char._1;
&opp._&usr_char._L3=&opp(of &usr_char._1-&usr_char._3);
&opp._&usr_char._L6=&opp(of &usr_char._1-&usr_char._6);
&opp._&usr_char._L9=&opp(of &usr_char._1-&usr_char._9);
&opp._&usr_char._L12=&opp(of &usr_char._1-&usr_char._12);
%end;
%do bb=1 %to %words(&max_variables.);
%let usr_char=%scan(&max_variables.,&bb);
%let opp=max;
&opp._&usr_char._L1=&usr_char._1;
&opp._&usr_char._L3=&opp(of &usr_char._1-&usr_char._3);
&opp._&usr_char._L6=&opp(of &usr_char._1-&usr_char._6);
&opp._&usr_char._L9=&opp(of &usr_char._1-&usr_char._9);
&opp._&usr_char._L12=&opp(of &usr_char._1-&usr_char._12);
%end;
%do cc=1 %to %words(&min_variables.);
%let usr_char=%scan(&min_variables.,&cc);
%let opp=min;
&opp._&usr_char._L1=&usr_char._1;
&opp._&usr_char._L3=&opp(of &usr_char._1-&usr_char._3);
&opp._&usr_char._L6=&opp(of &usr_char._1-&usr_char._6);
&opp._&usr_char._L9=&opp(of &usr_char._1-&usr_char._9);
&opp._&usr_char._L12=&opp(of &usr_char._1-&usr_char._12);
%end;
run;
%mend Create_chars;
%let varlist=n_cust;
%let charlist=A_BAL_CUR A_CADV K_DAY_DLNQT_CUR;
%let mean_variables = A_BAL_CUR A_CADV;
%let max_variables=k_day_DLNQT_CUR a_cadv;

You might also like