Compare state Medicaid drug utilization in Python
Compare quarterly prescriptions and pre-rebate reimbursement for an NDC across states using real samples and copyable Python.
Compare one drug package across states and quarters
A pharmacy analyst wants to compare recorded use of the same NDC in two Medicaid managed-care programs. This example selects 00002143380, labeled TRULICITY in CMS's short product-name field, and compares North Carolina and Virginia across the four quarters of 2025. The product supplies prescription counts, reimbursement and explicit reporting status at that exact level.
The optional input.csv contains the two real state/NDC/program selections. They also appear in the selections list in the Python code below. No observations are invented.
Download state_utilization.csv into your working folder. This is a sample of the paid product, retaining all quarters and both program types for this NDC in the two selected states, with every product column unchanged. The code selects managed care (MCOU); fee-for-service records remain available in the sample for a separate comparison.
Code example
Run this in a notebook cell, IPython or a Python script. It uses the Python standard library and reads the file one row at a time, so the same code works with the full download. Keep the 11-digit NDC as text.
Paths are relative to your working folder; use from pathlib import Path; print(Path.cwd()) to check it, or supply full paths.
import csv
from decimal import Decimal
# Real NDC and states selected for this comparison, also in example_input.csv.
selections = [
{"state_abbr": "NC", "ndc": "00002143380", "program": "MCOU"},
{"state_abbr": "VA", "ndc": "00002143380", "program": "MCOU"},
]
wanted = {(r["state_abbr"], r["ndc"], r["program"]) for r in selections}
selected = {}
with open("state_utilization.csv", newline="", encoding="utf-8") as f:
for row in csv.DictReader(f):
key = (row["state_abbr"], row["ndc"], row["program"])
if key in wanted:
period = (key, row["quarter"])
if period in selected:
raise ValueError("Duplicate comparison key")
selected[period] = row
results = []
for choice in selections:
key = (choice["state_abbr"], choice["ndc"], choice["program"])
for quarter in ("1", "2", "3", "4"):
row = selected.get((key, quarter))
status = row["value_status"] if row else "no_row_in_source"
result = {**choice, "quarter": quarter, "status": status,
"prescriptions": "", "total_reimbursed_usd": "",
"reimbursement_per_prescription_usd": ""}
if status == "reported":
result["prescriptions"] = row["prescriptions"]
result["total_reimbursed_usd"] = row["total_reimbursed_usd"]
count = Decimal(row["prescriptions"])
if count > 0:
result["reimbursement_per_prescription_usd"] = str(
(Decimal(row["total_reimbursed_usd"]) / count).quantize(Decimal("0.01")))
results.append(result)
print("State Quarter Prescriptions Reimbursement USD USD/prescription Status")
for row in results:
print(f"{row['state_abbr']:<7}{row['quarter']:<9}{row['prescriptions'] or '—':>13}"
f"{row['total_reimbursed_usd'] or '—':>19}"
f"{row['reimbursement_per_prescription_usd'] or '—':>18} {row['status']}")
Replace the selections list with your own state/NDC/program records, or read the same columns from a CSV with csv.DictReader. Select FFSU for fee-for-service. The full key includes year, which is fixed to 2025 in this product.
Analysis of the output
State Quarter Prescriptions Reimbursement USD USD/prescription Status
NC 1 2387 2499318.54 1047.05 reported
NC 2 2310 2459171.80 1064.58 reported
NC 3 2211 2380943.09 1076.86 reported
NC 4 2157 2300151.19 1066.37 reported
VA 1 9054 8735295.00 964.80 reported
VA 2 8751 8437911.16 964.22 reported
VA 3 8897 8583428.18 964.76 reported
VA 4 8333 8018687.61 962.28 reported
For this NDC, North Carolina's reported managed-care prescriptions decline from 2,387 in the first quarter to 2,157 in the fourth. Virginia reports 9,054 and 8,333 respectively. These are counts of prescriptions for one package code, not unique patients, all prescriptions for the brand, or measures adjusted for state enrollment.
The derived reimbursement per prescription divides the published total amount by the reported prescription count for the same record. North Carolina's fourth-quarter value is $1,066.37; Virginia's is $962.28. Reimbursement includes dispensing fees and is before rebates. Differences can reflect payment arrangements and prescription quantities; this ratio is not a net drug price or an acquisition cost.
The code leaves derived amounts blank for suppressed or missing records and for a zero prescription denominator. In the full product, suppressed preserves all five withheld numeric cells. CMS uses low-count and secondary suppression, so the flag alone does not identify a count. An absent record does not establish zero utilization. Do not recover suppressed values by subtracting national or other totals.
Using the full dataset
Extract the purchased ZIP and run the code with its state_utilization.csv, replacing the sample file. The full state table contains 4,987,938 records across the 50 states, DC and Puerto Rico. Coverage varies by program: coverage.csv distinguishes 384 observed geography/quarter/program combinations from 40 absent combinations across state and national records.
The five CSV tables also include 320,609 published national records, source-field definitions, and the destination of every original source row. National records use XX and are kept in national_utilization.csv. Do not add them to states or expect visible state sums to reproduce published national amounts. The archive includes metadata, validation results, source hashes and this working example.
For acquisition-cost comparisons, inspect 2025 NADAC history and its unit and date fields before joining on NDC. A drug-description or classification join can use FDA NDC reference or NDC → ATC, with explicit checks for historical coverage and multiple classifications. Our national annual Medicaid spending history uses different drug-name groupings and inputs; exact reconciliation is not promised. The drug-cost selection guide helps choose among them.
Sources and versions
Source: CMS public State Drug Utilization Data, 2025, updated July 13, 2026 and retained by ACD October 23, 2026 UTC. All four 2025 quarters are represented, including later source revisions. This is not a live feed or a record of what was available at each quarter end.
The free source is public-domain government data. ACD sells prepared tables, coverage accounting, documentation, validation and delivery. Read the CMS definitions and suppression guidance and our source and preparation record.
Example checked October 23, 2026 against both these samples and the complete purchased files. Source and processing records, sample checksums · Expected result CSV. Independent preparation; no agency endorsement.