This lesson collects a few practical programming patterns that appear frequently in clinical SAS work
The focus is not on one procedure but on reusable logic patterns, such as creating shell rows, identifying complete pre- and post-treatment records, and comparing current rows with previous rows
These examples are intentionally small so the programming idea is easy to isolate and understand
Create dummy rows for shells or layout templates
In reporting work we often need rows to exist even before real result values are joined in. The following examples show different ways to create those shell rows.
Using explicit output statements
This is the most direct method when only a few rows are needed
Each `output` writes one row with the manually assigned values
SAS Log
The dataset contains one observation for each shell row we coded explicitly
Dataset View
Using DATALINES
This is convenient when the shell values are easier to list, like a small text table
The delimiter separates the values for each variable
SAS Log
Dataset View
Using PROC FORMAT with a DO loop
This method is useful when coded values already map naturally to display text
The format stores the mapping, and the loop generates the possible coded values
SAS Log
Dataset View
Using PROC SQL
SQL can also create small shell datasets when the values are known in advance
This style is useful when the surrounding workflow is already SQL-based
SAS Log
Dataset View
Using DO loops to generate patterned rows
Loops are especially useful when shell rows follow a repeated structure
This is common for visits, treatment groups, and parameter-level shells
SAS Log
Inspect the three loop-based datasets and confirm how nested loops generate all combinations of the listed values
Dataset View
Check whether a subject has both pretreatment and post-treatment non-missing results
Create example data
SAS Log
The objective is to identify subject-parameter combinations that have at least one non-missing pretreatment result and at least one non-missing post-treatment result
Dataset View
Using PROC SQL with separate pre- and post-tables followed by an INNER JOIN
First create one dataset for non-missing pretreatment records and another for non-missing post-treatment records
Then join them on subject and parameter
SAS Log
Using PROC SQL with INTERSECT
This approach is shorter when the two sets have the same columns and only common rows are needed
SAS Log
Using PROC SQL with a subquery
The subquery method filters pretreatment rows to only those subject-parameter combinations that also exist post-treatment
SAS Log
Dataset View
Select all records for subject-parameter combinations that satisfy the rule
Once the qualifying subject-parameter combinations are identified, we can join them back to the original dataset to keep all related rows
SAS Log
Dataset View
Using PROC SORT NODUPKEY and MERGE
This approach uses standard DATA step-style processing instead of SQL
It is often useful when the surrounding program is already sort- and merge-based
SAS Log
Dataset View
Carry a previous row value forward for comparison with the current row
Check whether the current start date equals the previous end date
Create sample data
SAS Log
Using the LAG function
`lag` can bring the value from the previous execution of the function call
It should be used carefully because it is queue-based, not simply row-based
Reset logic is still needed at BY group boundaries
SAS Log
Dataset View
Using RETAIN instead of LAG
The retained variable method is often easier to reason about in clinical programming
After checking the current row, the previous end date is updated for the next row
SAS Log
Compare `se02` and `se03` and decide which logic is easier for you to maintain and explain
Dataset View
Key points to remember
Common logic patterns often reappear across many projects, so it is worth learning them as reusable templates
There are usually multiple ways to solve the same requirement in SAS, such as SQL versus sort and merge
Choose the approach that is clearest, safest, and easiest to validate in your programming context