Kirk Lafler Shares 25 Coding Techniques!

25 Best Practice Coding Techniques for SAS Users By Kirk Paul Lafler, Software Intelligence Corporation

As SAS software becomes increasingly more popular, best practice coding techniques and guidelines become ever so critical. SAS software provides users with a powerful programming language for accessing, analyzing, manipulating, and presenting data. This tip addresses useful coding techniques for all operating system platforms.

  • After running a SAS program, immediately review the SAS log for notes, warnings, and error messages. Avoid turning off SAS System options that turn off SAS log notes, messages, and warnings.
  • Turn on the SOURCE2 SAS System option to display included source code on the log. Best practice coding techniques should mandate inclusion and display of any and all information that is available during a SAS session.
  • Considering procedures like PROC SQL and PROC REPORT for code simplification. Because multiple processes can be frequently accomplished in a single procedure step, I/O may be reduced.
    When a DATA step or PROC can do the same job, consider using procedures whenever possible. Procedures are tried-and-proven throughout the world’s SAS installations, testing requirements is considerably less.
  • Create user-defined format libraries to store formatted values in one place. User-defined format libraries have the added advantage of making programs easier to maintain since formatted data values are not hard coded.
  • Include RUN statements at the end of each DATA or PROC step (to separate step boundaries) to print benchmark statistics on the SAS log immediately following each step.
  • Document programs and routines with comments. In addition to the value associated with explaining program logic, comments should provide important information about complex code and logic conditions in a program. This helps to document important program processes as well as minimizes the learning curve associated with program maintenance and enhancement for other users.
  • Assign descriptive and meaningful variable names. Besides improving the readability of program code, it serves an important element in the form of documentation.
  • Construct program header information to serve as program documentation for all programs. The following example illustrates the type of information that can be added so others have a useful documented history.
  • Simplify complex code and operations into smaller, more manageable parts. By splitting complex code into two or more programming statements, a program becomes easier to read as well as more maintainable.
  • Specify SAS data set names when invoking procedures to help improve documentation efforts as well as preventing an incorrect data set from being processed.
  • Utilize macros for redundant code and enable autocall processing by specifying the MAUTOSOURCE system option.
  • Create macro libraries to store common macro routines in one place.
  • Create permanent libraries containing information from daily, weekly, monthly, quarterly, and annual runs. The type of libraries consists of scripts, SAS programs, SAS logs, output lists, and documentation of instructions for others to follow.
  • Create views based on user input to simplify and streamline redundant, complex and/or burdensome tasks. Consider creating views in a central view library to support maintenance and documentation requirements.
  • Code for unknown data values. This will prevent unassigned or null data values from falling through logic conditions.
  • Store informats, formats, and labels with the SAS data sets that use them. Informats, formats, and labels should be stored with important SAS data sets to minimize processing time. An important reason for using this technique is that many popular procedures use stored formats and labels as they produce output, eliminating the need to assign them in each individual step. This provides added incentives and value for programmers and users, especially since reporting requirements are usually time critical.
  • Construct conditions that would render data unusable and abort (or end) the program. This prevents unwanted or harmful data from being processed or written to a data set.
  • Test program code using “complete” test data particularly if the data set is small or represents a random sample of a large data set.
  • Set OBS=0 to test syntax and compile time errors without the risk of executing any observations through a DATA or PROC step.
  • Use the PROC SQL VALIDATE clause to test syntax and compile time errors in PROC SQL code.
  • Specify the NOREPLACE system option to prevent permanent SAS data sets from accidentally being overwritten while writing or testing a program.
  • Take advantage of procedures that summarize large amounts of data by saving and using the results in order to avoid reading a large data set again.
  • Add options that are frequently used into the SAS configuration file. This eliminates the time and keystrokes necessary to enter them during a SAS session.
  • Add statements that are frequently used into the SAS autoexec file. This eliminates the time and keystrokes necessary to enter them during a SAS session.
  • Source: 25 Best Practice Coding Techniques for SAS Users By Kirk Paul Lafler, Software Intelligence Corporation

    Advertisements

    OpenClinica 3.1: conditional CRF: showing or hiding items, based on input

    Short instructional video to show how conditional logic works in OpenClinica 3.1

    FAIR USE-
    “Copyright Disclaimer Under Section 107 of the Copyright Act 1976, allowance is made for “fair use” for purposes such as criticism, comment, news reporting, teaching, scholarship, and research. Fair use is a use permitted by copyright statute that might otherwise be infringing. Non-profit, educational or personal use tips the balance in favor of fair use.”

    Source: OpenClinica

    SAS Cheat Sheet – Part 4

    INFORMATS

    Code Description
    <w.d> Reads standard numeric data
    <datew.> Reads date values (ddmmmyy)/td>;
    <$w.> Reads standard character data
    <$VARYINGw> Reads character data of varying length

    SAS represents a date internally as the number of days
    between January 1, 1960 and the specified date. Dates
    before 1960 are represented with negative numbers. A
    SAS date value is a numeric variable.

    SAS DateValue Calendar DateValue
    0 January 1, 1960
    30 January 31, 1960
    366 January 1, 1961
    -365 January 1, 1959

    How do you use an InFormat to create a SAS date?

    If the raw fields are aligned in columns, use formatted input and specify the informat.
    e.g. input @1 visitdt yymmdd8.;

    With list input, use a separate INFORMAT statement or a modifier.
    informat visitdtyymmdd8.;
    input visitdt;
    input visitdt : yymmdd8.;

    How do you refer to a particular date in SAS?

    To create a SAS date constant, write the date enclosed in single or double quotes, followed by a D.
    e.g. age = ’20-MAR-2012’d – incdt;
    where visitdt lt “1may12”D;

    How do you work with SAS date values?

    When a variable is a SAS date value, you can easily apply operations such as addition and subtraction. To find the number of days between two dates, simply subtract the two SAS date variables.
    e.g. daysbtwn = visitdt1 – visitdt2;

    Comparison operators can also be used.
    if visitdt1 <; visitdt2 then do;

    Building a study in OpenClinica

    In less than 7 minutes we show you how to build a basic study in OpenClinica, so you can experiment.
    The CRF that was used in this video can be found at http://trialdatasolutions.com/tds/CRFExamples/

    FAIR USE-
    “Copyright Disclaimer Under Section 107 of the Copyright Act 1976, allowance is made for “fair use” for purposes such as criticism, comment, news reporting, teaching, scholarship, and research. Fair use is a use permitted by copyright statute that might otherwise be infringing. Non-profit, educational or personal use tips the balance in favor of fair use.”

    Source: OpenClinica

    OpenClinica: from zero to CRF

    Designing a CRF with repeating items in OpenClinica: step-by-step-instructions.
    The CRF in this video can be found at http://trialdatasolutions.com/tds/CRFExamples/

    FAIR USE-
    “Copyright Disclaimer Under Section 107 of the Copyright Act 1976, allowance is made for “fair use” for purposes such as criticism, comment, news reporting, teaching, scholarship, and research. Fair use is a use permitted by copyright statute that might otherwise be infringing. Non-profit, educational or personal use tips the balance in favor of fair use.”

    Source: OpenClinica

    ePRO-Deployment-Basics

    Learn about ePRO deployment basics for your clinical trial – watch this webinar snippet from CRF Health.

    FAIR USE-
    “Copyright Disclaimer Under Section 107 of the Copyright Act 1976, allowance is made for “fair use” for purposes such as criticism, comment, news reporting, teaching, scholarship, and research. Fair use is a use permitted by copyright statute that might otherwise be infringing. Non-profit, educational or personal use tips the balance in favor of fair use.”

    Source: CRF Health

    SAS Cheat Sheet – Part 6

    The macro facility is a tool you can use in your programming./*

    Macro Language

    %DO macro-var=start_value %TO end_value

    %DO %WHILE (expression); /*Executes a section of a macro repetitively while a condition is true*/

    %DO %UNTIL (expression); /*Executes a section of a macro repetitively until a condition is true*/

    Macro variables can be stored in either the global symbol table or in a local symbol table

    %GLOBAL macro-variable(s); /*Creates macro variables that are available during the execution of an entire SAS session*/

    %IF expression %THEN action; <%ELSE action;> /Conditionally process a portion of a macro*/

    %LENGTH (character string | text expression) /*Returns the length of a string*/

    The name assigned to a macro variable must be a valid SAS name

    %LET macro-variable =<value>; /*Creates a macro variable and assigns it a value*/

    %MACRO mname (<pp1><…,ppn><kp1=value<..<kpn=v>);/*Begins a macro definition*/

    %MEND <macro-name>; /*Ends a macro definition*/

    %SCAN(argument,n<,delimiters>)/*Search for a word that is specified by its position in a string*/

    %SUBSTR (argument,position<,length>)/*Produce a substring of a character string*/

    %UPCASE (character string | text expression) /*Convert values to uppercase*/

    Macro variable values are text values

    Macro Quoting

    %QUOTE | %NRQUOTE and %BQUOTE | %NRBQUOTE /*Mask special characters and mnemonic operators in a resolved value at macro execution */

    %STR | %NRSTR /*Mask special characters and mnemonic operators in constant text at macro compilation */

    %SUPERQ /*Masks special characters/mnemonic operators at macro execution but prevents further resolution of the value*/

    Example:

    *----------------------------------------------------------------*//* This macro will produce summary statistics ----------------   */

    %Macro safety1;
    %odscmd (start,portnum=11,rptname=AEActivityReport);
    Data _null_;
    thedate=today();
    fstdate = mdy(month(thedate), 1, year(thedate));
    lstdate = intnx(‘month’, fstdate, 1)-1;
    call symput (‘strtdate’, put(fstdate, date9.));
    call symput (‘stpdate’, put(thedate, date9.));
    run;

    %put strtdate: &strtdate;
    %put stpdate: &stpdate;
    * more code goes here;

    /*----------------------------------------------------------------*/

    SAS Cheat Sheet – Part 3

    FORMATS

    Code Description
    <w.d> standard numeric
    <COMMAw.d> writes numeric values with commas and decimal points
    <Zw.d> print leading zeros
    <$w.> writes standard character data
    <$CHARw.> writes standard character data
    <$VARYINGw.> Writes character data of varying length

    Format is used to change how the values of variables are displayed/portrayed in your SAS output. A more formal term for this process is “altering the external representation of the values of variables in a SAS data set.

    So how to add leading zeros ? e.g., 999 –> 0999

    In order to create variable LEADZEROS_ID, you can use the PUT function and the format Zw. which adds leading zeros to the specified length. Here’s an example:

    leadzeros_id=put(id,z4.);

    data leadzeros;

    input ID PETNAME $ SEX $ AGE WEIGHT;
    datalines;
    1 Mark M 1 2.5
    2 Ace M 3 44
    3 Fuzzy M 2 18
    4 Champ M 4 55
    5 Tom M 6 63.5 62.5
    6 Ivon M 7 83
    7 Balboa M 12 64.5
    ;
    run;
    data leadzeros;
    set leadzeros;
    leadzeros_num = put(id,z4.); *z4. format is used so 3 leading zeros will be added if the ID length is 1;
    run;

    Another common format is the COMMAw.d: this format writes numeric values with commas that separate every three digits and a period that separates the decimal fraction.

    w = specifies the width of the output field
    d = optionally specifies the number of digits to the right of the decimal point in the numeric value

    e.g. put @10 profit comma10.2;
    so this number 45678.45 will be formatted to 45,678.45

    SQL Basics – Part 2: Create Tables and Variables and more

    We’ll dive into CREATE command. We’ll ‘Create’ Tables, Temp Tables, Table Variables and Variables. We are going to ‘Insert’ data into this table (INSERT INTO) and we’ll occasionally ‘DROP’ the tables

    FAIR USE-
    “Copyright Disclaimer Under Section 107 of the Copyright Act 1976, allowance is made for “fair use” for purposes such as criticism, comment, news reporting, teaching, scholarship, and research. Fair use is a use permitted by copyright statute that might otherwise be infringing. Non-profit, educational or personal use tips the balance in favor of fair use.”

    SQL Basics – Part 1: Select, From, Where

    SQL Basics – Part 1: Visual demonstration of Select, From and Where clauses

    FAIR USE-
    “Copyright Disclaimer Under Section 107 of the Copyright Act 1976, allowance is made for “fair use” for purposes such as criticism, comment, news reporting, teaching, scholarship, and research. Fair use is a use permitted by copyright statute that might otherwise be infringing. Non-profit, educational or personal use tips the balance in favor of fair use.”