sas2typst_report is a SAS macro package that enables direct generation of Typst (.typ) files from within SAS. Typst is a modern, declarative typesetting system designed for fast and precise document layout. This package allows users to write native Typst code inline in SAS programs and seamlessly stream SAS dataset contents into Typst tables.
data ADSL;
length SUBJID SITEID REGION $200. AGE 8. SEX RACE ETHNIC $200. ;
SUBJID="101-301"; SITEID="101"; REGION="North America"; AGE=57;SEX= "F";RACE= "White";ETHNIC="Not Hispanic or Latino"; WEIGHTBL=58.1; HEIGHTBL=160.8;output;
SUBJID="102-303"; SITEID="102"; REGION="North America"; AGE=61;SEX= "M";RACE= "White";ETHNIC="Not Reported"; WEIGHTBL=104.9; HEIGHTBL=188.0;output;
SUBJID="111-303"; SITEID="111"; REGION="North America"; AGE=43;SEX= "M";RACE= "White";ETHNIC="Not Hispanic or Latino"; WEIGHTBL=85.5; HEIGHTBL=182.5;output;
run;
%typst_start(outdir = ., outfile=Listing_1_1.typ)
#set page(paper: "a4", flipped: true,
header: [#grid(columns: (1fr, 1fr), align: top,
[#align(left)[Protocol: ABC-123]],
[#align(right)[Page #context counter(page).display()]],
)],
)
&nw;
= Listing 1. Demographics and Baseline Characteristics
&nw;
#text(size: 10pt)[Safety Analysis Population]
&nw;
#table(
columns: (
18mm,
10mm,
28mm,
15mm,
10mm,
18mm,
52mm,
26mm,
26mm
),
stroke: none,
table.header(
table.hline(stroke: 1pt + black),
"Subject",
"Site",
"Region",
"Age (years)",
"Sex",
"Race",
"Ethnic",
"Baseline Weight~(kg)",
"Baseline Height~(cm)",
table.hline(stroke: 1pt + black),
),
%typ_dataset_csv(ds=ADSL,wh=%nrbquote(SEX="M"),varlist=SUBJID SITEID REGION AGE SEX RACE ETHNIC WEIGHTBL HEIGHTBL)
table.footer(
table.hline(stroke: 1pt + black),
),
)
#v(-15pt)
#text(size: 8pt)[
Age is calculated at baseline.
#par()[Baseline weight/height are measured at Visit 1 (Day 1).]
]
%typst_end();The generated .typ file can be previewed using the web-based viewer, from which you can also download the output as a PDF.
https://typst.app/play/

Alternatively, after installing Typst via the Command Prompt or PowerShell using
winget install --id Typst.Typst
you can generate a PDF file by running
typst compile Listing_1_1.typ
on your local machine.
For installation methods on operating systems other than Windows, as well as various ways to run Typst, please refer to the official documentation: https://typst.app/docs/
In developing this package, we referred to an excellent paper by a pioneering contributor who has already applied Typst to document and report generation in the pharmaceutical domain. -- Tao Zhang (Phasta), "Superiority in Nature: Typst in Production of TLFs" https://www.lexjansen.com/pharmasug-cn/2024/AP/Pharmasug-China-2024-AP10043_Final_Paper.pdf.
Purpose: Initialize Typst source file generation using PROC STREAM and prepare the output environment.
outdir = Output directory (currently not used)
outfile = Name of the Typst output file (default: sample.typ)
nw = Line break token for Typst output.
In general, native Typst code can be written directly as-is. However, due to Typst syntax requirements, explicit line breaks must be inserted using &nw at positions where a line break is required.
%typst_start(outfile=my_listing.typ);
<Typst source code>
%typst_end();Finalize Typst source file generation and properly close PROC STREAM output.
Terminates PROC STREAM using explicit delimiter (;;;;)
Clears the associated FILENAME reference
Must be used in conjunction with typst_start
%typst_start(outfile=my_listing.typ);
<Typst source code>
%typst_end();Purpose: Extract rows from a SAS dataset based on a WHERE condition and output the values in a CSV-like format that can be directly embedded into a Typst table().
ds = Input SAS dataset name
wh = WHERE condition to filter observations (use %nrbquote if needed)
varlist = List of variables to output (space-delimited)
Outputs comma-separated values suitable for Typst tables
All values are quoted
Double quotation marks (") inside character variables are replaced with Unicode right double quotation mark (U+201D)
A warning is issued if such replacement occurs
%typ_dataset_csv(ds=sashelp.class,wh=%nrbquote(AGE<=15),varlist=NAME AGE SEX WEIGHT)0.1.0(25December2025): Initial version
The package is built on top of SAS Packages Framework(SPF) developed by Bartosz Jablonski.
For more information about the framework, see SAS Packages Framework.
You can also find more SAS Packages (SASPacs) in the SAS Packages Archive(SASPAC).
First, create a directory for your packages and assign a packages fileref to it.
filename packages "\path\to\your\packages";Secondly, enable the SAS Packages Framework. (If you don't have SAS Packages Framework installed, follow the instruction in SPF documentation to install SAS Packages Framework.)
%include packages(SPFinit.sas)Install SAS package you want to use with the SPF's %installPackage() macro.
-
For packages located in SAS Packages Archive(SASPAC) run:
%installPackage(packageName)
-
For packages located in PharmaForest run:
%installPackage(packageName, mirror=PharmaForest)
-
For packages located at some network location run:
%installPackage(packageName, sourcePath=https://some/internet/location/for/packages)
(e.g.
%installPackage(ABC, sourcePath=https://github.com/SomeRepo/ABC/raw/main/))
Load SAS package you want to use with the SPF's %loadPackage() macro.
%loadPackage(packageName)