SlideShare a Scribd company logo
1 of 4
Tips and Tricks for SAS® Program Automation
Adam Hood @ Slalom Consulting
2
Use PROC PRINTTO to put all logs in a designated
directory. This will allow any team member to quickly
find and troubleshoot problems in processes. All too
often teams spend time trying to find the appropriate
logs for a given process.
To further enhance your log files, add a timestamp for
easy sorting and a naming convention for specific
process identification.
Tips and Tricks for SAS® Program Automation
Adam Hood
Slalom Consulting
Abstract
Automating SAS programs can be difficult and result in
overcoming unexpected obstacles. Often automation
forces us to account for edge case scenarios that we
would normally assume won’t be encountered.
The goal of this poster is to help programmers
overcome the ‘gotcha’s associated with building and
troubleshooting automated programs. We will touch
on 3 areas: logging, basic error handling, and system
options.
Logging Tip #1
proc format ;
picture myfmt low-high = '%0Y%0m%0d%0H%0M%0S'
(datatype = datetime) ;
run ;
%let DTstamp =%sysfunc(datetime(),myfmt.);
proc printto log=“/home/ahood/logs/myprocess_&DTstamp..log” new;
Often there are multiple stakeholders for a given
automated job. Emails are a great way to let others know
when jobs finish. However, if you want a more self-
service approach or you want to build dependencies with
other systems, job run logs stored in a database may be
the answer. This allows other systems to access and
report on job status.
An important part of successfully implementing a busy
job run log is to limit logging to inserts. Updates and
deletes should be avoided if possible. Best practice
would be to create a database view that filtered the data
to the last status per process per day (or process run).
The DTstamp macro variable from tip #1 makes a great
process identifier.
Logging Tip #2
Table: job_run_log
View: job_run_log_v
CREATE TABLE job_run_log
( process_id bigint NOT NULL, process_name VARCHAR(100),
status VARCHAR(50), note VARCHAR(255),
rcrd_create_ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (process_id, rcrd_create_ts) )
CREATE VIEW job_run_log_v as
select a.* from test.job_run_log a
join (
select process_id, max(rcrd_create_ts) as last_ts
from test.job_run_log
group by 1
) b on a.process_id = b.process_id and a.rcrd_create_ts = b.last_ts
;
Often we need to understand when something
happened at the second level. This is especially true
for long running jobs.
To aid in this effort, we add timestamps to the SAS log
for key events or interactions.
Logging Tip #3
%macro timestamp_it();
/* Note that the format statement can be moved to the settings files for
the job */
proc format;
picture DTstamp
other='%Y-%0m-%0d %0H:%0M:%0S' (datatype=datetime);
run;
%put NOTE: %sysfunc(datetime(),DTstamp.);
%mend;
%timestamp_it;
3
MLOGIC – This option is different from MPRINT in that
it displays the logic that is being executed. Therefore,
if you have for loops or conditional statements,
MLOGIC will help understand the execution.
SYMBOLGEN – Have you ever had to include %PUT
statements just to know what the macro variables
resolve to? SYMBOLGEN does that for you. It outputs
the value of macro variables as they are used.
FULLSTIMER – Briefly, FULLSTIMER outputs
performance statistics for each step in a SAS job. This
helps debug long running queries, etc.
Locked datasets can cause problems if there are
potentially multiple processes accessing them. We
have removed this by locking a dataset first, accessing
or updating, and then unlocking. Here are examples of
how this works from PharmaSUG2005.
Tips and Tricks for SAS® Program Automation
Adam Hood
Slalom Consulting
Error Handling Tip #1
There are occasions where missing data can cause a
domino effect of downstream errors. We find it
helpful to check the rows returned by critical queries
so an error can be thrown immediately instead of
downstream. Here is an example macro for just that.
Error Handling Tip #2 System Option Tips
SAS Community Tips – Adding a date and time stamp
to the SAS Log -
http://www.sascommunity.org/wiki/Tips:Adding_a_da
te_and_time_stamp_message_to_the_SAS_Log
The Big Introduction from the Smallest Macro -
http://www2.sas.com/proceedings/sugi31/038-31.pdf
Automating your SAS jobs, Paul Anderson, SGF 2009 -
http://support.sas.com/resources/papers/proceedings
09/184-2009.pdf
Play Nice with Others: Waiting for a Lock on SAS Table,
John Leveille and Jimmy Hunnings, PharmaSUG 2005 -
http://www.lexjansen.com/pharmasug/2005/posters/
po33.pdf
References
%macro check_sql;
%if &sqlobs.=0 %then %do;
%put ERROR: SQL Query returned no rows.;
%end;
%else %do;
%put NOTE: SQL Checked.;
%end;
%mend check_sql;
%macro trylock(member=,timeout=10,retry=1);
%local starttime;
%let starttime = %sysfunc(datetime());
%do %until(&syslckrc <= 0
or %sysevalf(%sysfunc(datetime()) > (&starttime + &timeout)));
%put trying open ...;
data _null_;
dsid = 0;
do until (dsid > 0 or datetime() > (&starttime + &timeout));
dsid = open("&member");
if (dsid = 0) then rc = sleep(&retry);
end;
if (dsid > 0) then rc = close(dsid);
run;
%put trying lock ...;
lock &member;
%put syslckrc=&syslckrc;
%end;
%mend trylock;
Debugging is critical when working with automated
programs. Our approach is to turn on the debugging
options in the settings file for the job. This produces
larger logs, but we overcome this with an aggressive log
retention policy.
There are 4 options that we use to help debug programs.
MPRINT – If you use macros in any significant way, this
will help you debug. MPRINT will print the macro as it is
included in the code.
Sgf15 tips and tricks for sas program automation-final post1

More Related Content

Similar to Sgf15 tips and tricks for sas program automation-final post1

20 Windows Tools Every SysAdmin Should Know
20 Windows Tools Every SysAdmin Should Know20 Windows Tools Every SysAdmin Should Know
20 Windows Tools Every SysAdmin Should KnowPower Admin LLC
 
Welcome Webinar Slides
Welcome Webinar SlidesWelcome Webinar Slides
Welcome Webinar SlidesSumo Logic
 
Habits of Effective SAS Programmers
Habits of Effective SAS ProgrammersHabits of Effective SAS Programmers
Habits of Effective SAS ProgrammersSunil Gupta
 
How Manual Testers Can Break into Automation Without Programming Skills
How Manual Testers Can Break into Automation Without Programming SkillsHow Manual Testers Can Break into Automation Without Programming Skills
How Manual Testers Can Break into Automation Without Programming SkillsRanorex
 
SAP ADMINISTRATION
SAP ADMINISTRATIONSAP ADMINISTRATION
SAP ADMINISTRATIONAly Adel
 
File Processing - Batch Process Execution
File Processing - Batch Process ExecutionFile Processing - Batch Process Execution
File Processing - Batch Process ExecutionAbimael Desales López
 
File Processing - Process Execution Solution
File Processing - Process Execution SolutionFile Processing - Process Execution Solution
File Processing - Process Execution SolutionAbimael Desales López
 
automation process
automation processautomation process
automation processDhiraj Jha
 
Ibm ims tools san ramon oct 2011
Ibm ims tools san ramon oct 2011Ibm ims tools san ramon oct 2011
Ibm ims tools san ramon oct 2011evgeni77
 
How to Create a Runbook: A Guide for Sysadmins & MSPs
How to Create a Runbook: A Guide for Sysadmins & MSPsHow to Create a Runbook: A Guide for Sysadmins & MSPs
How to Create a Runbook: A Guide for Sysadmins & MSPsLizzyManz
 
Prog1 chap1 and chap 2
Prog1 chap1 and chap 2Prog1 chap1 and chap 2
Prog1 chap1 and chap 2rowensCap
 
10 best-practices-for-new-robotic-process-automation-rpa-developer-220f7ccdf33
10 best-practices-for-new-robotic-process-automation-rpa-developer-220f7ccdf3310 best-practices-for-new-robotic-process-automation-rpa-developer-220f7ccdf33
10 best-practices-for-new-robotic-process-automation-rpa-developer-220f7ccdf33Srinivas Kannan
 
20061122 JBoss-World Experiences with JBoss jBPM
20061122 JBoss-World Experiences with JBoss jBPM20061122 JBoss-World Experiences with JBoss jBPM
20061122 JBoss-World Experiences with JBoss jBPMcamunda services GmbH
 
Mind the gap - Troopers 2016
Mind the gap  - Troopers 2016Mind the gap  - Troopers 2016
Mind the gap - Troopers 2016Casey Smith
 
Tracing and profiling my sql (percona live europe 2019) draft_1
Tracing and profiling my sql (percona live europe 2019) draft_1Tracing and profiling my sql (percona live europe 2019) draft_1
Tracing and profiling my sql (percona live europe 2019) draft_1Valerii Kravchuk
 
Instrumenting plugins for Performance Schema
Instrumenting plugins for Performance SchemaInstrumenting plugins for Performance Schema
Instrumenting plugins for Performance SchemaMark Leith
 

Similar to Sgf15 tips and tricks for sas program automation-final post1 (20)

20 Windows Tools Every SysAdmin Should Know
20 Windows Tools Every SysAdmin Should Know20 Windows Tools Every SysAdmin Should Know
20 Windows Tools Every SysAdmin Should Know
 
Welcome Webinar Slides
Welcome Webinar SlidesWelcome Webinar Slides
Welcome Webinar Slides
 
Habits of Effective SAS Programmers
Habits of Effective SAS ProgrammersHabits of Effective SAS Programmers
Habits of Effective SAS Programmers
 
pm1
pm1pm1
pm1
 
How Manual Testers Can Break into Automation Without Programming Skills
How Manual Testers Can Break into Automation Without Programming SkillsHow Manual Testers Can Break into Automation Without Programming Skills
How Manual Testers Can Break into Automation Without Programming Skills
 
Carasik BPM ECM
Carasik BPM ECMCarasik BPM ECM
Carasik BPM ECM
 
SAP ADMINISTRATION
SAP ADMINISTRATIONSAP ADMINISTRATION
SAP ADMINISTRATION
 
File Processing - Batch Process Execution
File Processing - Batch Process ExecutionFile Processing - Batch Process Execution
File Processing - Batch Process Execution
 
File Processing - Process Execution Solution
File Processing - Process Execution SolutionFile Processing - Process Execution Solution
File Processing - Process Execution Solution
 
automation process
automation processautomation process
automation process
 
Ibm ims tools san ramon oct 2011
Ibm ims tools san ramon oct 2011Ibm ims tools san ramon oct 2011
Ibm ims tools san ramon oct 2011
 
How to Create a Runbook: A Guide for Sysadmins & MSPs
How to Create a Runbook: A Guide for Sysadmins & MSPsHow to Create a Runbook: A Guide for Sysadmins & MSPs
How to Create a Runbook: A Guide for Sysadmins & MSPs
 
11i Logs
11i Logs11i Logs
11i Logs
 
Prog1 chap1 and chap 2
Prog1 chap1 and chap 2Prog1 chap1 and chap 2
Prog1 chap1 and chap 2
 
10 best-practices-for-new-robotic-process-automation-rpa-developer-220f7ccdf33
10 best-practices-for-new-robotic-process-automation-rpa-developer-220f7ccdf3310 best-practices-for-new-robotic-process-automation-rpa-developer-220f7ccdf33
10 best-practices-for-new-robotic-process-automation-rpa-developer-220f7ccdf33
 
20061122 JBoss-World Experiences with JBoss jBPM
20061122 JBoss-World Experiences with JBoss jBPM20061122 JBoss-World Experiences with JBoss jBPM
20061122 JBoss-World Experiences with JBoss jBPM
 
Mind the gap - Troopers 2016
Mind the gap  - Troopers 2016Mind the gap  - Troopers 2016
Mind the gap - Troopers 2016
 
RAMP_FINAL_ppt
RAMP_FINAL_pptRAMP_FINAL_ppt
RAMP_FINAL_ppt
 
Tracing and profiling my sql (percona live europe 2019) draft_1
Tracing and profiling my sql (percona live europe 2019) draft_1Tracing and profiling my sql (percona live europe 2019) draft_1
Tracing and profiling my sql (percona live europe 2019) draft_1
 
Instrumenting plugins for Performance Schema
Instrumenting plugins for Performance SchemaInstrumenting plugins for Performance Schema
Instrumenting plugins for Performance Schema
 

Recently uploaded

Call Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service BangaloreCall Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangaloreamitlee9823
 
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...SUHANI PANDEY
 
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...amitlee9823
 
Call Girls In Bellandur ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bellandur ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bellandur ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bellandur ☎ 7737669865 🥵 Book Your One night Standamitlee9823
 
BigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptxBigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptxolyaivanovalion
 
Accredited-Transport-Cooperatives-Jan-2021-Web.pdf
Accredited-Transport-Cooperatives-Jan-2021-Web.pdfAccredited-Transport-Cooperatives-Jan-2021-Web.pdf
Accredited-Transport-Cooperatives-Jan-2021-Web.pdfadriantubila
 
Generative AI on Enterprise Cloud with NiFi and Milvus
Generative AI on Enterprise Cloud with NiFi and MilvusGenerative AI on Enterprise Cloud with NiFi and Milvus
Generative AI on Enterprise Cloud with NiFi and MilvusTimothy Spann
 
April 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's AnalysisApril 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's Analysismanisha194592
 
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...amitlee9823
 
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteedamy56318795
 
Call Girls Jalahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...
Call Girls Jalahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...Call Girls Jalahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...
Call Girls Jalahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...amitlee9823
 
Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...amitlee9823
 
Thane Call Girls 7091864438 Call Girls in Thane Escort service book now -
Thane Call Girls 7091864438 Call Girls in Thane Escort service book now -Thane Call Girls 7091864438 Call Girls in Thane Escort service book now -
Thane Call Girls 7091864438 Call Girls in Thane Escort service book now -Pooja Nehwal
 
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...amitlee9823
 
Probability Grade 10 Third Quarter Lessons
Probability Grade 10 Third Quarter LessonsProbability Grade 10 Third Quarter Lessons
Probability Grade 10 Third Quarter LessonsJoseMangaJr1
 

Recently uploaded (20)

Call Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service BangaloreCall Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
Call Girls Begur Just Call 👗 7737669865 👗 Top Class Call Girl Service Bangalore
 
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
VIP Model Call Girls Hinjewadi ( Pune ) Call ON 8005736733 Starting From 5K t...
 
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Indiranagar Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
 
Predicting Loan Approval: A Data Science Project
Predicting Loan Approval: A Data Science ProjectPredicting Loan Approval: A Data Science Project
Predicting Loan Approval: A Data Science Project
 
Call Girls In Bellandur ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bellandur ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bellandur ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bellandur ☎ 7737669865 🥵 Book Your One night Stand
 
BigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptxBigBuy dropshipping via API with DroFx.pptx
BigBuy dropshipping via API with DroFx.pptx
 
Accredited-Transport-Cooperatives-Jan-2021-Web.pdf
Accredited-Transport-Cooperatives-Jan-2021-Web.pdfAccredited-Transport-Cooperatives-Jan-2021-Web.pdf
Accredited-Transport-Cooperatives-Jan-2021-Web.pdf
 
(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7
(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7
(NEHA) Call Girls Katra Call Now 8617697112 Katra Escorts 24x7
 
Generative AI on Enterprise Cloud with NiFi and Milvus
Generative AI on Enterprise Cloud with NiFi and MilvusGenerative AI on Enterprise Cloud with NiFi and Milvus
Generative AI on Enterprise Cloud with NiFi and Milvus
 
April 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's AnalysisApril 2024 - Crypto Market Report's Analysis
April 2024 - Crypto Market Report's Analysis
 
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Saket (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
Call Girls Bommasandra Just Call 👗 7737669865 👗 Top Class Call Girl Service B...
 
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
5CL-ADBA,5cladba, Chinese supplier, safety is guaranteed
 
Call Girls Jalahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...
Call Girls Jalahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...Call Girls Jalahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...
Call Girls Jalahalli Just Call 👗 7737669865 👗 Top Class Call Girl Service Ban...
 
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get CytotecAbortion pills in Doha Qatar (+966572737505 ! Get Cytotec
Abortion pills in Doha Qatar (+966572737505 ! Get Cytotec
 
Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...
Vip Mumbai Call Girls Thane West Call On 9920725232 With Body to body massage...
 
Thane Call Girls 7091864438 Call Girls in Thane Escort service book now -
Thane Call Girls 7091864438 Call Girls in Thane Escort service book now -Thane Call Girls 7091864438 Call Girls in Thane Escort service book now -
Thane Call Girls 7091864438 Call Girls in Thane Escort service book now -
 
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
Call Girls Hsr Layout Just Call 👗 7737669865 👗 Top Class Call Girl Service Ba...
 
Probability Grade 10 Third Quarter Lessons
Probability Grade 10 Third Quarter LessonsProbability Grade 10 Third Quarter Lessons
Probability Grade 10 Third Quarter Lessons
 
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts ServiceCall Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
Call Girls In Shalimar Bagh ( Delhi) 9953330565 Escorts Service
 

Sgf15 tips and tricks for sas program automation-final post1

  • 1. Tips and Tricks for SAS® Program Automation Adam Hood @ Slalom Consulting
  • 2. 2 Use PROC PRINTTO to put all logs in a designated directory. This will allow any team member to quickly find and troubleshoot problems in processes. All too often teams spend time trying to find the appropriate logs for a given process. To further enhance your log files, add a timestamp for easy sorting and a naming convention for specific process identification. Tips and Tricks for SAS® Program Automation Adam Hood Slalom Consulting Abstract Automating SAS programs can be difficult and result in overcoming unexpected obstacles. Often automation forces us to account for edge case scenarios that we would normally assume won’t be encountered. The goal of this poster is to help programmers overcome the ‘gotcha’s associated with building and troubleshooting automated programs. We will touch on 3 areas: logging, basic error handling, and system options. Logging Tip #1 proc format ; picture myfmt low-high = '%0Y%0m%0d%0H%0M%0S' (datatype = datetime) ; run ; %let DTstamp =%sysfunc(datetime(),myfmt.); proc printto log=“/home/ahood/logs/myprocess_&DTstamp..log” new; Often there are multiple stakeholders for a given automated job. Emails are a great way to let others know when jobs finish. However, if you want a more self- service approach or you want to build dependencies with other systems, job run logs stored in a database may be the answer. This allows other systems to access and report on job status. An important part of successfully implementing a busy job run log is to limit logging to inserts. Updates and deletes should be avoided if possible. Best practice would be to create a database view that filtered the data to the last status per process per day (or process run). The DTstamp macro variable from tip #1 makes a great process identifier. Logging Tip #2 Table: job_run_log View: job_run_log_v CREATE TABLE job_run_log ( process_id bigint NOT NULL, process_name VARCHAR(100), status VARCHAR(50), note VARCHAR(255), rcrd_create_ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (process_id, rcrd_create_ts) ) CREATE VIEW job_run_log_v as select a.* from test.job_run_log a join ( select process_id, max(rcrd_create_ts) as last_ts from test.job_run_log group by 1 ) b on a.process_id = b.process_id and a.rcrd_create_ts = b.last_ts ; Often we need to understand when something happened at the second level. This is especially true for long running jobs. To aid in this effort, we add timestamps to the SAS log for key events or interactions. Logging Tip #3 %macro timestamp_it(); /* Note that the format statement can be moved to the settings files for the job */ proc format; picture DTstamp other='%Y-%0m-%0d %0H:%0M:%0S' (datatype=datetime); run; %put NOTE: %sysfunc(datetime(),DTstamp.); %mend; %timestamp_it;
  • 3. 3 MLOGIC – This option is different from MPRINT in that it displays the logic that is being executed. Therefore, if you have for loops or conditional statements, MLOGIC will help understand the execution. SYMBOLGEN – Have you ever had to include %PUT statements just to know what the macro variables resolve to? SYMBOLGEN does that for you. It outputs the value of macro variables as they are used. FULLSTIMER – Briefly, FULLSTIMER outputs performance statistics for each step in a SAS job. This helps debug long running queries, etc. Locked datasets can cause problems if there are potentially multiple processes accessing them. We have removed this by locking a dataset first, accessing or updating, and then unlocking. Here are examples of how this works from PharmaSUG2005. Tips and Tricks for SAS® Program Automation Adam Hood Slalom Consulting Error Handling Tip #1 There are occasions where missing data can cause a domino effect of downstream errors. We find it helpful to check the rows returned by critical queries so an error can be thrown immediately instead of downstream. Here is an example macro for just that. Error Handling Tip #2 System Option Tips SAS Community Tips – Adding a date and time stamp to the SAS Log - http://www.sascommunity.org/wiki/Tips:Adding_a_da te_and_time_stamp_message_to_the_SAS_Log The Big Introduction from the Smallest Macro - http://www2.sas.com/proceedings/sugi31/038-31.pdf Automating your SAS jobs, Paul Anderson, SGF 2009 - http://support.sas.com/resources/papers/proceedings 09/184-2009.pdf Play Nice with Others: Waiting for a Lock on SAS Table, John Leveille and Jimmy Hunnings, PharmaSUG 2005 - http://www.lexjansen.com/pharmasug/2005/posters/ po33.pdf References %macro check_sql; %if &sqlobs.=0 %then %do; %put ERROR: SQL Query returned no rows.; %end; %else %do; %put NOTE: SQL Checked.; %end; %mend check_sql; %macro trylock(member=,timeout=10,retry=1); %local starttime; %let starttime = %sysfunc(datetime()); %do %until(&syslckrc <= 0 or %sysevalf(%sysfunc(datetime()) > (&starttime + &timeout))); %put trying open ...; data _null_; dsid = 0; do until (dsid > 0 or datetime() > (&starttime + &timeout)); dsid = open("&member"); if (dsid = 0) then rc = sleep(&retry); end; if (dsid > 0) then rc = close(dsid); run; %put trying lock ...; lock &member; %put syslckrc=&syslckrc; %end; %mend trylock; Debugging is critical when working with automated programs. Our approach is to turn on the debugging options in the settings file for the job. This produces larger logs, but we overcome this with an aggressive log retention policy. There are 4 options that we use to help debug programs. MPRINT – If you use macros in any significant way, this will help you debug. MPRINT will print the macro as it is included in the code.