writing maintainable code with ‘style’ allan page senior marketing analyst canadian tire bank
TRANSCRIPT
![Page 1: Writing Maintainable Code with ‘Style’ Allan Page Senior Marketing Analyst Canadian Tire Bank](https://reader034.vdocuments.us/reader034/viewer/2022050714/56649e3b5503460f94b2d2ea/html5/thumbnails/1.jpg)
WritingMaintainable Code with ‘Style’
Allan PageSenior Marketing AnalystCanadian Tire Bank
![Page 2: Writing Maintainable Code with ‘Style’ Allan Page Senior Marketing Analyst Canadian Tire Bank](https://reader034.vdocuments.us/reader034/viewer/2022050714/56649e3b5503460f94b2d2ea/html5/thumbnails/2.jpg)
Valid SAS Code can be hard to read!%let acxiom=ctracx1;options comamid=tcp remote=acxiom;filename rlink'!sasroot\connect\saslink\tcpunix.scr';signon acxiom;rsubmit;%letpath_name = ~/prod/strategic_initiatives/000425;libname u "&path_name";proc sort data=u.b2b_summary nodupkey out=b2b_summary;byacct_id;run;proc sort data=u.fusion nodupkey out=fusion;byacct_id;run;proc sort data=u.promo nodupkey out=promo;byacct_id;run;data u.all;merge promo(in=a) b2b_summary(in=b)fusion(in=c);by acct_id;if a then mailed = 1;if b thenresponded=1;if responded=1 and fusion_grp=' ' then fusion_grp='NotScored';if daily_average_bal ne 0 and daily_average_bal ne . thenactivated = 1;run;proc summary data=u.all nway;class fusion_grp;varmailed responded activated;output out=u.stats(drop= _type_ _freq_)sum=;run;
![Page 3: Writing Maintainable Code with ‘Style’ Allan Page Senior Marketing Analyst Canadian Tire Bank](https://reader034.vdocuments.us/reader034/viewer/2022050714/56649e3b5503460f94b2d2ea/html5/thumbnails/3.jpg)
Starting each statement on a new line helps%let acxiom=ctracx1;options comamid=tcp remote=acxiom;filename rlink '!sasroot\connect\saslink\tcpunix.scr';signon acxiom;rsubmit;%let path_name = ~/prod/strategic_initiatives/000425;libname u "&path_name";proc sort data=u.b2b_summary nodupkey out=b2b_summary;by acct_id;run;proc sort data=u.fusion nodupkey out=fusion;by acct_id;run;proc sort data=u.promo nodupkey out=promo;by acct_id; run;data u.all;merge promo(in=a) b2b_summary(in=b) fusion(in=c);by acct_id;if a then mailed = 1;if b then responded = 1;if responded=1 and fusion_grp=' ' then fusion_grp='Not Scored';if daily_average_bal ne 0 and daily_average_bal ne . then activated =1;run;proc summary data=u.all nway;class fusion_grp;var mailed responded activated;output out=u.stats(drop= _type_ _freq_) sum=;run;
![Page 4: Writing Maintainable Code with ‘Style’ Allan Page Senior Marketing Analyst Canadian Tire Bank](https://reader034.vdocuments.us/reader034/viewer/2022050714/56649e3b5503460f94b2d2ea/html5/thumbnails/4.jpg)
Blank lines are good for step separation.%let acxiom=ctracx1;options comamid=tcp remote=acxiom;filename rlink '!sasroot\connect\saslink\tcpunix.scr';signon acxiom;rsubmit;
%let path_name = ~/prod/strategic_initiatives/000425;libname u "&path_name";
proc sort data=u.b2b_summary nodupkey out=b2b_summary;by acct_id;run;proc sort data=u.fusion nodupkey out=fusion;by acct_id;run;proc sort data=u.promo nodupkey out=promo;by acct_id; run;
data u.all;merge promo(in=a) b2b_summary(in=b) fusion(in=c);by acct_id;if a then mailed = 1;if b then responded = 1;if responded=1 and fusion_grp=' ' then fusion_grp='Not Scored';if daily_average_bal ne 0 and daily_average_bal ne . then activated = 1;run;
proc summary data=u.all nway;class fusion_grp;var mailed responded activated;output out=u.stats(drop= _type_ _freq_) sum=;run;
![Page 5: Writing Maintainable Code with ‘Style’ Allan Page Senior Marketing Analyst Canadian Tire Bank](https://reader034.vdocuments.us/reader034/viewer/2022050714/56649e3b5503460f94b2d2ea/html5/thumbnails/5.jpg)
Indentation helps define processes within a step.%let acxiom=ctracx1;
options comamid=tcp remote=acxiom;filename rlink '!sasroot\connect\saslink\tcpunix.scr';signon acxiom;rsubmit;
%let path_name = ~/prod/strategic_initiatives/000425;libname u "&path_name";
proc sort data=u.b2b_summary nodupkey out=b2b_summary;by acct_id;run;proc sort data=u.fusion nodupkey out=fusion;by acct_id;run;proc sort data=u.promo nodupkey out=promo;by acct_id; run;
data u.all;merge promo(in=a) b2b_summary(in=b) fusion(in=c);by acct_id;if a then mailed = 1;if b then responded = 1;if responded=1 and fusion_grp=' ' then fusion_grp='Not Scored';if daily_average_bal ne 0 and daily_average_bal ne . then activated = 1;
run;
proc summary data=u.all nway;class fusion_grp;var mailed responded activated;output out=u.stats(drop= _type_ _freq_) sum=;
run;
![Page 6: Writing Maintainable Code with ‘Style’ Allan Page Senior Marketing Analyst Canadian Tire Bank](https://reader034.vdocuments.us/reader034/viewer/2022050714/56649e3b5503460f94b2d2ea/html5/thumbnails/6.jpg)
Indentation is good with do/end groups too.%let acxiom=ctracx1;options comamid=tcp remote=acxiom;filename rlink '!sasroot\connect\saslink\tcpunix.scr';signon acxiom;rsubmit;
%let path_name = ~/prod/strategic_initiatives/000425;libname u "&path_name";
proc sort data=u.b2b_summary nodupkey out=b2b_summary; by acct_id;run;proc sort data=u.fusion nodupkey out=fusion; by acct_id;run;proc sort data=u.promo nodupkey out=promo; by acct_id;run;
data u.all;merge promo(in=a) b2b_summary(in=b) fusion(in=c);by acct_id;if a then mailed = 1;
if b then do;responded = 1;
end;
if responded=1 and fusion_grp=' ' then fusion_grp='Not Scored';if daily_average_bal ne 0 and daily_average_bal ne . then activated = 1;
run;
proc summary data=u.all nway;. . . . more SAS code;
run;
![Page 7: Writing Maintainable Code with ‘Style’ Allan Page Senior Marketing Analyst Canadian Tire Bank](https://reader034.vdocuments.us/reader034/viewer/2022050714/56649e3b5503460f94b2d2ea/html5/thumbnails/7.jpg)
Use tab stops to line up repeated code.%let acxiom=ctracx1;options comamid=tcp remote=acxiom;filename rlink '!sasroot\connect\saslink\tcpunix.scr';signon acxiom;rsubmit;
%let path_name = ~/prod/strategic_initiatives/000425;libname u "&path_name";
proc sort data=u.b2b_summary nodupkey out=b2b_summary; by acct_id;run;proc sort data=u.fusion nodupkey out=fusion; by acct_id;run;proc sort data=u.promo nodupkey out=promo; by acct_id;run;
data u.all;. . . . more data lines;
run;
proc summary data=u.all nway;class fusion_grp;var mailed responded activated;output out=u.stats(drop= _type_ _freq_)
sum=;run;
![Page 8: Writing Maintainable Code with ‘Style’ Allan Page Senior Marketing Analyst Canadian Tire Bank](https://reader034.vdocuments.us/reader034/viewer/2022050714/56649e3b5503460f94b2d2ea/html5/thumbnails/8.jpg)
Add comments to say what’s happening.************************************;* Connect to Remote System ;************************************;
%let acxiom=ctracx1;options comamid=tcp remote=acxiom;filename rlink '!sasroot\connect\saslink\tcpunix.scr';signon acxiom;rsubmit;
************************;* Assign libref ;************************;
%let path_name = ~/prod/strategic_initiatives/000425;libname u "&path_name";
proc sort data=u.b2b_summary nodupkey out=b2b_summary; by acct_id;run;proc sort data=u.fusion nodupkey out=fusion; by acct_id;run;proc sort data=u.promo nodupkey out=promo; by acct_id;run;
data u.all;. . . . more data lines;
run;
proc summary data=u.all nway;. . . . more proc lines;
run;
![Page 9: Writing Maintainable Code with ‘Style’ Allan Page Senior Marketing Analyst Canadian Tire Bank](https://reader034.vdocuments.us/reader034/viewer/2022050714/56649e3b5503460f94b2d2ea/html5/thumbnails/9.jpg)
Comments have different styles.%let acxiom=ctracx1;
. . . . more SAS lines;rsubmit;
%let path_name = ~/prod/strategic_initiatives/000425;libname u "&path_name";
proc sort data=u.b2b_summary nodupkey out=b2b_summary;by acct_id;run;proc sort data=u.fusion nodupkey out=fusion;by acct_id;run;proc sort data=u.promo nodupkey out=promo;by acct_id; run;
data u.all;merge promo(in=a) b2b_summary(in=b) fusion(in=c);
by acct_id;if a then mailed = 1; * Assign value here ;if b /* Assign value here */then responded = 1;
if responded=1 and fusion_grp=' ' then fusion_grp='Not Scored';if daily_average_bal ne 0 and daily_average_bal ne . then activated = 1;
run;/* This code won’t run at allproc summary data=u.all nway;
. . . . more SAS lines;run;*/
![Page 10: Writing Maintainable Code with ‘Style’ Allan Page Senior Marketing Analyst Canadian Tire Bank](https://reader034.vdocuments.us/reader034/viewer/2022050714/56649e3b5503460f94b2d2ea/html5/thumbnails/10.jpg)
A misplaced comment!1 data one;NOTE: SCL source line.2 set sasuser.whse *comment;; - 22 ------- 202ERROR: File WORK.COMMENT.DATA does not exist.ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string, (, ;, END, KEY, KEYS, NOBS, OPEN, POINT, _DATA_, _LAST_, _NULL_.
ERROR 202-322: The option or parameter is not recognized and will be ignored.
3 run;
NOTE: The SAS System stopped processing this step because of errors.WARNING: The data set WORK.ONE may be incomplete. When this step was stopped there were 0 observations and 3 variables.
NOTE: DATA statement used: real time 0.18 seconds cpu time 0.09 seconds
![Page 11: Writing Maintainable Code with ‘Style’ Allan Page Senior Marketing Analyst Canadian Tire Bank](https://reader034.vdocuments.us/reader034/viewer/2022050714/56649e3b5503460f94b2d2ea/html5/thumbnails/11.jpg)
Number Code Sections/*=====================================================================+| trs07.sas 20 || Set the initial file. |+======================================================================*/data _null_; set exp; call symput ("mtoday",today()); call symput ("topics",0);
-- more code --
/*=====================================================================+| trs07.sas 30 || Global statements for macros. |+======================================================================*/%global asterisk tq crse instn bbase form1 partone parttwo parthre partfiv partsix partsvn partegt partnin partten parteln parttwv partthr part813 wtblnk quizname webcrse webct dsn;
![Page 12: Writing Maintainable Code with ‘Style’ Allan Page Senior Marketing Analyst Canadian Tire Bank](https://reader034.vdocuments.us/reader034/viewer/2022050714/56649e3b5503460f94b2d2ea/html5/thumbnails/12.jpg)
Use numbers in program names.
Program names END START would not sort in their required run sequence.
Program names 001STARTand 002END would be properly ordered.
![Page 13: Writing Maintainable Code with ‘Style’ Allan Page Senior Marketing Analyst Canadian Tire Bank](https://reader034.vdocuments.us/reader034/viewer/2022050714/56649e3b5503460f94b2d2ea/html5/thumbnails/13.jpg)
Use date functions instead of hard-coding dates in your programs.
data _null_; call symput('lstmth',put(intnx('month',date(),-1,'end'),date9.));run;
%put lstmth= &lstmth;
lstmth= 30SEP2004
![Page 14: Writing Maintainable Code with ‘Style’ Allan Page Senior Marketing Analyst Canadian Tire Bank](https://reader034.vdocuments.us/reader034/viewer/2022050714/56649e3b5503460f94b2d2ea/html5/thumbnails/14.jpg)
Questions or Comments?
![Page 15: Writing Maintainable Code with ‘Style’ Allan Page Senior Marketing Analyst Canadian Tire Bank](https://reader034.vdocuments.us/reader034/viewer/2022050714/56649e3b5503460f94b2d2ea/html5/thumbnails/15.jpg)
Copyright © 2003, SAS Institute Inc. All rights reserved. 15