How to Program the Demographics Table in SAS (A Step-by-Step Guide)

How to Program the Demographics Table in SAS (A Step-by-Step Guide)

If you’re starting your career as a statistical programmer in the pharmaceutical industry, there’s one task you are guaranteed to face early on: building the demographics-table.

This is a foundational component of all clinical-study-reports (CSR). Before a regulatory reviewer looks at whether a new drug actually worked, they look here. They need to verify that the subjects in the clinical-trials are well-balanced across treatment groups and represent the intended population.

But taking raw data and turning it into a polished, submission-ready table is a rite of passage that trips up many new programmers.

The Agitation: Why It’s Harder Than It Looks

The biggest mistake junior programmers make is underestimating the data preparation phase. They get eager and try to jump straight into formatting the final output.

But raw data collected via case-report-forms-crf is almost never analysis-ready.

For instance, you might have a patient’s Date of Birth and the date they were diagnosed, but you don’t have their actual Age. Gender and Race are often collected as numeric codes (e.g., 1 for Male, 2 for Female) in the database, not the readable text a reviewer expects to see on the page.

Furthermore, statisticians usually require an “All Patients” or “Overall” column to summarize the entire study population—a column that doesn’t naturally exist in your raw dataset. Add in strict precision rules (like ensuring the mean is displayed to exactly one more decimal place than the raw data), and it’s easy to see why initial drafts often get rejected for multiple rounds of stressful rework.

The Solution: A Systematic Framework

To avoid the endless loop of revisions, you need a systematic approach. Think of building your demographics-table as an assembly line.

Here is the step-by-step framework I recommend to get it right the first time:

Step 1: Import and Derive

First, get your raw CRF data into SAS. Once imported, your immediate goal is derivation.
Calculate Age: You’ll need to compute the continuous age variable, usually by subtracting the Date of Birth from a reference date (like informed consent or diagnosis date) and dividing by 365.25.
Decode Categories: Use PROC FORMAT to turn those cryptic numeric codes for Sex and Race into readable text strings (“Male”, “Female”, “Caucasian”, etc.).

Step 2: Create the “All Patients” Group

This is a crucial structural step. Your data currently has subjects split by their assigned treatment groups (e.g., Treatment A and Treatment B). To create the “Overall” summary column without writing complex, repetitive code, use explicit-output in a Data step. By outputting each record twice—once with its original treatment group and once assigned to a new “All Patients” group—you seamlessly set up your data for the upcoming statistical procedures.

Step 3: Crunch the Numbers

Now that your data structure is perfect, let SAS do the heavy lifting.
Continuous Data: Run proc-means to calculate the N, Mean, Standard Deviation, Median, Minimum, and Maximum for Age.
Categorical Data: Run proc-freq to get the counts (n) and percentages (%) for categorical variables like Sex and Race.

Step 4: Reformatting and Precision

Raw statistical output isn’t pretty. You need to format these numbers according to the Statistical Analysis Plan (SAP).
– Apply the precision rules to your PROC MEANS output (e.g., rounding the mean to one decimal place).
– Concatenate your counts and percentages for PROC FREQ into a single string (e.g., 15 (50.0%)).
– Use macro-variables via PROC SQL to dynamically capture the total population counts (the “N=xx” in your column headers).

Step 5: Stack and Report

Finally, bring it all together. Stack your Age, Sex, and Race summaries into one unified dataset. Assign ordering variables (ord, subord) to ensure Age always appears before Sex, and Sex before Race.

With your data perfectly structured, ordered, and formatted, you can finally use proc-report to generate the final output, adding the necessary titles and footnotes.

By following this assembly line approach—Derive, Expand, Crunch, Format, and Report—you’ll build robust, accurate tables that pass statistician review on the first try.

More at made2sticklearning.org/clinicalsas