Good Programming Practices

Mise en forme du programme

Utiliser des indentations :

  • Évitez d’avoir dix espaces pour une indentations. La largeur de la ligne de code visible à l’écran est limitée. Avec de nombreux espaces pour une indentation, le code s’étale sur la ligne et peut excéder la largeur visible à l’écran. Il est alors nécessaire de faire de constant va-et-vient pour lire le programme, ce qui est fastidieux et n’apporte aucune valeur.
  • Dans la mesure du possible, restez cohérent dans vos indentations. Si vous mettez une indentation avant l’instruction set dans une étape data, n’en mettez pas deux dans l’étape data suivante.

Gestion des modifications

Modifier un programme

  • Si vous devez modifier un programme important, pensez à faire une copie du programme originale avant de réaliser des modifications. Appelez votre programme d’origine <mypgm>_original.sas par exemple, ou encore <mypgms>_old.sas. Vous serez alors en mesure de retourner à la version initiale, de comparer la nouvelle version à la version initiale à tout moment.

Quality Insurance

GPP https://www.scriptrunner.com/en/blog/5-powershell-/
https://poshcode.gitbook.io/powershell-practice-and-style/introduction/readme
arrêtez de vous tirer une balle dans le pied

GPP initialiser une variable

So easy to get it wrong

proc transpose without a var statement

proc transpose without an id statement

proc transpose with formats

Language

The accepted language(s) for comments and names (file name, dataset name, variable name, macro parameter name….) is/are … [company specific]. xxformat GmbH uses English (American) as official language.

Prevent issues

Use brackets

You should never have to write things, but if you have to then please use brackets.

data one;

var1=(var2=var3);

run;

Debugging

Ensure that all ERROR, WARNING and critical notes are fixed on an ongoing basis.

Always debug the first issue first.

Don’t hide the messages by default in production mode. Fix the issue or hide them locally for a specific step/proc and comment about it.

in development mode only, you could consider nofmterr if all required formats are not yet available. This would help to focus on other issues.

options varlenchk=

Variable Length

Always specify the length of a character variable.

Use the default length for numeric variable, which is 8 (bytes), the maximum.

A smaller length can cause issues by proc cport/cimport

Procedures

Always specify data=, even in a proc print.

Quotes

Prefer single quotes when both single and double quotes are possible.

Case

Prefer lowercase. Save uppercase for specific cases to emphasize.

Macro Variable

Always add a final dot when calling a macro variable.

Macro Definition

Specify the name of the macro in %mend.

Don’t define a macro within another macro definition.

Program Development

Create small examples to figure out the syntax

When the log does not provide you the information

Improve the readability of the log

Avoid misleading messages

Example %str(ERR)OR

Only display the word ERROR only if there is really an error.

Global Options

Versioning

Version 0.1 is the first draft version

Version 1.0 is the first release version

Version 1.1 is the first update after first release (hotfix, minor changes).

Naming Convention

Be aware of SAS Naming Conventions/Rules

Start the name of the views with the letter v.

Anticipate Compatibility Between Operating Environements whenever possible

  • File names: Use lowercase, avoid accentuated letters (e.g. é, ç, ß…), avoid blanks

Define naming rules

Example : Reserve :: for the program header

Example : [XX]

Define a keyword which must not exist in the final version of the program.

For example, I use XXVERO. It helps tagging sections to update.

Naming Test Cases / Edit checks

Prefer a meaningful name rather than numbers

Numbering Convention

Improve the Readability of the Program

Line break

Ensure that the code is not too wide. The user should not have to use the move to go from left to right in order to be able to see the code.

Align do et end

Indentations

Add sections/subsections, and number them

Overwriting the data – Merge (high concern)

Comment properly to debug easily

Prefer ; (%) in the core of the program in order to be able to use /**/ to comment out large section.

It is still ok to use /for program header/ at the beginning of the program

or for examples of macro call at the end of the program.

%* Within a Macro

%*——————————;

and not

*————————————;

Example of bug with %else

Use the right syntax (low concern)

Rule: Don’t use a dot when defining a length. The dot is only relevant for formats.

Why are people confused?

When the length of a character variable is not specified, the SAS System uses the format width to set the length.

The length fo the x variable is 2 in this example.

data one; format x $2.; x=’A’; run;

What we would rather have is a length statement without a dot.

data one; length x $2; x=’A’; run;

So the syntax between the two is mixed up:

data one; length x $2.; (wrong) x=’A’; run;

Highlight: format width is used as length

proc format; value $alphabet ‘A’=’Alpha’; run;

data one; format x $alphabet.; x=’A’; run;

proc contents data=one;

run;

Adding the length statement allows us

proc format; value $alphabet ‘A’=’Alpha’; run;

data one; length x $2; format x $alphabet.; x=’A’; run;

Macro Programming

Check as many information you can before abording a program.

This way the end user can fix multiple problems at once without having to rerun the program many times.

Exemple

%let xxstop=0;

%if….%then

%do;

 %put….

 %let xxstop=1;

%end;

%if….%then

%do;

 %put….

 %let xxstop=1;

%end;

%if &xxstop.=1 %then %goto pgm_end;

%pgm_end:

Add a debugging macro parameter

Conservative or not ?

Let’s take an example.

Up to 30 datasets should be available in the input library.

What if there are 31 datasets ?

  1. abord the program
  2. use the first 30 and add a warning in the program

Another example

Input library is not provided or invalid

  1. Abord the program
  2. Use the default work library

Writing comments

Macro/Tool – How to reduce the number of feature / simplify

rather than being tempted to always add more.

options fmterr;

pas de message quand un format manque une valeur

hidden truncations!

  • format manquant une valeur et réduit la longueur d’une variable