BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
don_mcmahon
Calcite | Level 5

I too use the approach of organizing my programs by import -> coding -> reports -> stats -> graphs.

 

To organize, each section and subsection starts with a comment with two asterisks in outline format. Example:

** 1.0 Import source data;

** 1.1 Import Questionnaires;

** 1.2 Import Labs;

...

** 2.0 Recode and transform data;

** 2.1 Collapse categories;

** 2.2 Normalize skewed distributions;

 

I then have a separate program that takes the name of a SAS program as it's input and then prints out the program outline as defined by the text following the double-asterisks. This is simple text parsing, but produces a document that provides a road map to the entire program.  It doesn't help with the problem of running specific sections of code. But helps a when you need to understand a program you wrote a long time ago.

 

...don

 

%macro pgmdoc(fn);
filename pgmfile "&fn";

data _pgmdoctemp_;
attrib Outline length=$255 format=$145.;
infile pgmfile linesize=255;
input Outline &;

if scan(upcase(Outline),1) ='DATA' then do;            Outline='        '||trim(Outline); output; end; else
if scan(upcase(Outline),1) ='PROC' then do;            Outline='        '||trim(Outline); output; end; else
if scan(upcase(Outline),1) ='SET' then do;            Outline='            '||trim(Outline); output; end; else
if scan(upcase(Outline),1) ='MERGE' then do;        Outline='            '||trim(Outline); output; end; else
if scan(upcase(Outline),1) ='LIBNAME' then do;         Outline='        '||trim(Outline); output; end; else
if substr(upcase(Outline),1,4)='%LET' then do;         Outline='        '||substr(Outline,1,length(outline)); output; end; else
if substr(upcase(Outline),1,8)='%INCLUDE' then do;  Outline='        '||substr(Outline,1,length(outline)); output; end; else
if substr(Outline,1,3)='** ' then do;                Outline=trim(substr(Outline,4,length(Outline)));output; end;
run;

title 'SAS Program Documenter';
title2 "&fn";
proc print data=_pgmdoctemp_ noobs;
var Outline;
run;
%mend;

 

Reeza
Super User

@don_mcmahon You may want to take a look at PROC SCAPROC if you aren't already familiar with it.

Peter_L
Quartz | Level 8

I use most of the methods mentioned by other contributors plus a few others.

.

Break code into cohesive sections.

Make the sections have as low a coupling as possible.

Use macro variables to parameterise code. There is a downside: too many global variables (= coupling).

.

Save code as "modules" and %include them as needed.

Write pre- and post- assertions for each module formally to define what it does.

Include the unit test code with every module commented out at the end.

.

I write the documentation in the code marked with special comment symbols of /** or **; A simple awk script then processes these into html in a similar way to javadoc.

.

Replace as much DATA step merge code with PROC SQL as possible. It makes the code shorter and easier to read.

Use data-driven methods as much as possible. This tends to make things simpler, shorter, more reliable and easier to test. SQL helps here.

Replace macro logic with DATA step logic and data-driven methods. DATA step logic is far easier to debug and quoting is not a problem.

.

To make your code easier to read change the editor to show a light grey background to comments. The colour extends to the screen width. Then use comments to break up sections of code. You will be surprised how big a difference it makes.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 17 replies
  • 4751 views
  • 22 likes
  • 10 in conversation