Look at data without writing code
Before you write a table program, know the data. Which values does a column have? Which records are odd? How many subjects are behind a count? Many programmers write a count() or a filter() for each question and delete it again. The data viewer answers with a few keys, and it writes the R condition for you. All keys are on the cheat sheet.
The data viewer and the data tools of this chapter are in the free Community edition.
21.1 Open a data frame
Section titled “21.1 Open a data frame”Run programs/21_1_severe.R once, so that adsl and adae are in the session. Then open one of them:
| How | What opens |
|---|---|
| F12 | the last data frame that a run made |
| Ctrl+Alt+D (Mac: Cmd+Alt+D) in an R file | the data frame at the cursor: adae, a column adae$AESEV, a step of a pipeline, a file path |
| a click in the Data view | that data frame; a click on a column opens it at that column |
| Open… in the toolbar of the viewer | another data frame of the session, or a data file (.xpt, .sas7bdat, .rds, .csv) |
Each data frame opens in its own editor tab, in pages of 100 rows. The data stays in R, so a large data frame stays quick. At the top right you always see n of N rows, k of K columns: how many rows the filters keep, and how many columns you show. F3, Shift+F4, Alt+W or Esc closes the viewer.
At the top is the WHERE box, then the toolbar: Columns, Find column…, Filters, Filter…, Copy, Export CSV, Views, R code, Keys, Summary, Labels, Raw, Open…. Each column name has a menu (the ▾, or a right-click). Rest the mouse on a column name to see its label, its type and how many values are missing.
21.2 The keys
Section titled “21.2 The keys”Press ? in the viewer to list every key. The ones you use every day:
| Key | What it does |
|---|---|
arrows, PageUp, PageDown, g |
move; go to a row number |
w |
the WHERE box: type an R condition |
/ or Ctrl+F (Mac: Cmd+F) |
search the shown columns for a text |
t |
the filter box under the column of the cell (21.4) |
f |
the values of the column with their counts and statistics |
| Enter / Shift+Enter | keep / remove the rows with the value of the cell |
| Ctrl+Alt+E | put column == value of the cell into the WHERE box |
| Ctrl+Alt+I | the value picker: tick values, the R condition is written as you tick |
| Ctrl+Alt+R | every row again (the sort stays) |
s, a, d |
sort by the column: toggle, ascending, descending |
c, h |
choose columns; hide the column of the cell |
l, v |
labels on or off; raw or formatted values |
m |
the Summary panel |
| Space | the row as a record |
The keys Ctrl+Alt+E, Ctrl+Alt+I, Ctrl+Alt+R and Ctrl+Alt+F use the Control key on a Mac too. The mouse can do everything as well: the toolbar, the ▾ of a column, a right-click.
21.3 A worked exploration: severe adverse events
Section titled “21.3 A worked exploration: severe adverse events”The statistician asks: how many subjects had a severe treatment-emergent adverse event, by treatment? You find the answer in the viewer first. Then you write the program.
- In the Data view, click
adae. What you should see: 1191 rows and 107 columns. - Press
w, typeTRTEMFL == "Y" & AESEV == "SEVERE"and press Enter. What you should see: 41 of 1191 rows. The condition is now a chip above the grid; its × removes it. - Click a cell of
TRT01Aand pressf. What you should see: Xanomeline Low Dose 25, Xanomeline High Dose 10, Placebo 6. These are records, not subjects. - Click a cell of
USUBJIDand pressf. What you should see: most subjects have 1 record, but some have more: 01-708-1019 and 01-708-1428 have 3 each. So there are fewer than 41 subjects. - Click a cell Placebo in
TRT01Aand press Ctrl+Alt+I. The picker opens with Placebo ticked. Press Enter. What you should see: the WHERE now also hasTRT01A == "Placebo", and 6 rows are left. - Press Ctrl+Alt+R to see every row again.
Now you know what the program must count: distinct subjects, not records. The program confirms it:
# Chapter 21: severe treatment-emergent adverse events, records and subjects per armlibrary(dplyr)
adsl <- pharmaverseadam::adsladae <- pharmaverseadam::adae
adae |> filter(TRTEMFL == "Y", AESEV == "SEVERE") |> group_by(TRT01A) |> summarise(records = n(), subjects = n_distinct(USUBJID))#> # A tibble: 3 x 3#> TRT01A records subjects#> <chr> <int> <int>#> 1 Placebo 6 5#> 2 Xanomeline High Dose 10 8#> 3 Xanomeline Low Dose 25 1641 records, 29 subjects. Placebo: 6 records of 5 subjects. In step 5, look at the 6 placebo rows: subject 01-710-1368 has two records of HIP FRACTURE (exercise 21-4). Chapter 26 shows a program that counts records instead of subjects, and how you find the mistake.
21.4 Filters: the value picker, the column filters and NA
Section titled “21.4 Filters: the value picker, the column filters and NA”The value picker (Ctrl+Alt+I on a cell, or f) lists the values of the column with their counts, the most frequent first. Its header gives the statistics: rows, values that are not missing, missing, distinct; for numbers also min, max, mean and SD. Tick values with a click or Space. As you tick, the R condition is written under the list with the number of rows it keeps. Enter keeps the ticked values, Shift+Enter removes them.
The column filters. The row under the column names has a box for each column. Press t to go to the box of the cell’s column, type a short form and press Enter. These are the forms of DataStep Studio for SAS:
| You type | On a text column | On a number or a date |
|---|---|---|
rash |
contains rash, any case |
equal |
=RASH |
equal | equal |
>65, <=65 |
compared as text | compared as numbers |
65-80 or 65..80 |
from 65 to 80 | |
RASH,FATIGUE |
one of these | one of these |
!RASH |
does not contain | not equal |
. or missing |
NA or "" |
NA |
01JAN2014 or 2014-01-01 |
that date |
NA is not a value. In R, NA == "Placebo" gives NA, not FALSE. The viewer knows this:
- In the picker,
NAis a row of its own. Tick it, and the condition isis.na(ASTDT). The viewer never writesASTDT == NA. - Shift+Enter on a Placebo cell writes
!(TRT01A %in% "Placebo"). That keeps the rows whereTRT01AisNA. The formTRT01A != "Placebo"would drop them. >65underAGEdoes not keep the rows whereAGEisNA.
Chapter 23 shows a program that loses records because of this rule.
The WHERE box takes any R condition. The column names complete as you type (Tab takes one). After ARM == ", the values of the column complete. A condition may also use another data frame of the session: USUBJID %in% adae$USUBJID[adae$AESER == "Y"]. When you type AGE = 65, the line under the box says that = assigns and == compares.
21.5 Find, sort, columns, and one subject
Section titled “21.5 Find, sort, columns, and one subject”- Find a column. Ctrl+Alt+F (or Find column…) and type part of a name or of a label:
relfindsAERELby its name andASTDYby its label Analysis Start Relative Day. Enter scrolls the grid to it. - Search the rows.
/, type a text, Enter: the rows where a shown column contains it. - Sort. A click on a column name sorts by it; a second click sorts descending; Shift+click adds a second sort column.
- Columns.
copens the chooser: tick, find, drag, Pin a column so that it stays on the left. - Summary (
m): a card for each shown column, computed on the rows that you see: missing values, distinct values, a histogram or the top values. A click on a bar is a filter. - One subject. A click on a
USUBJIDopens the subject across all data frames of the session.
In a terminal, dsr subject gives the same view of one subject. Here is the running example of the book, with adsl and adae in the session:
$ dsr subject 01-701-1015 --n 10Subject 01-701-1015
adae: 3 records AESEQ ASTDT AENDT ASTDY AEDECOD AESEV AESER AEREL AEOUT TRTEMFL 1 2014-01-03 NA 2 APPLICATION SITE ERYTHEMA MILD N PROBABLE NOT RECOVERED/NOT RESOLVED Y 2 2014-01-03 NA 2 APPLICATION SITE PRURITUS MILD N PROBABLE NOT RECOVERED/NOT RESOLVED Y 3 2014-01-09 2014-01-11 8 DIARRHOEA MILD N REMOTE RECOVERED/RESOLVED Y...Events in time order: 2014-01-02 adsl first dose 2014-01-03 adae APPLICATION SITE ERYTHEMA (mild) 2014-01-03 adae APPLICATION SITE PRURITUS (mild) 2014-01-09 adae DIARRHOEA (mild) 2014-07-02 adsl last dose 2014-07-02 adsl end of study: COMPLETED(The lines of the ADSL values are left out here.) Subject 01-701-1015 started placebo on 2 January 2014, had three mild adverse events in the first week, and completed the study.
21.6 The same answers in a terminal
Section titled “21.6 The same answers in a terminal”Each viewer action has a dsr command. They are useful in a script, for an AI agent (chapter 26), and when you want to paste the answer into a note.
| Viewer | Terminal |
|---|---|
| WHERE box, rows | dsr query adae --where '...' --vars '...' --sort '...' |
| how many rows | dsr query adae --where '...' --count |
f on a column |
dsr freq adae TRT01A --where '...' (--subjects counts subjects) |
| the value picker | dsr values adae AESEV --keep 'SEVERE,MODERATE' |
| a column filter | dsr filter adsl AGE '>80' |
| Find column | dsr describe adae --find rel |
| R code button | dsr query ... --code |
The question of 21.3, in three commands:
$ dsr freq adae TRT01A --where 'TRTEMFL == "Y" & AESEV == "SEVERE"'adae: 3 levels of TRT01A, 41 rows where TRTEMFL == "Y" & AESEV == "SEVERE" (pct: of these 41 rows that meet the WHERE, not of a population: the N of an arm comes from ADSL, dsr understand <data> --stats) TRT01A n pct Xanomeline Low Dose 25 61 Xanomeline High Dose 10 24.4 Placebo 6 14.6$ dsr freq adae TRT01A --where 'TRTEMFL == "Y" & AESEV == "SEVERE"' --subjectsadae: 3 levels of TRT01A, 29 subjects where TRTEMFL == "Y" & AESEV == "SEVERE" (pct: of these 29 subjects that meet the WHERE, not of a population: the N of an arm comes from ADSL, dsr understand <data> --stats) TRT01A n pct Xanomeline Low Dose 16 55.2 Xanomeline High Dose 8 27.6 Placebo 5 17.2$ dsr filter adae AEDECOD 'rash'grepl("rash", tolower(AEDECOD), fixed = TRUE)FILTER: 57 of 1191 rows | dsr query adae --where 'grepl("rash", tolower(AEDECOD), fixed = TRUE)'The R code button (or --code) turns what you see into code for your program, and checks that it gives the same rows:
$ dsr query adae --where 'TRTEMFL == "Y" & AESEV == "SEVERE"' --vars 'USUBJID AEDECOD' --sort USUBJID --code# dplyradae |> dplyr::filter(TRTEMFL == "Y" & AESEV == "SEVERE") |> dplyr::arrange(USUBJID) |> dplyr::select(USUBJID, AEDECOD)# base R(function(d) d[order(d$USUBJID), , drop = FALSE])(subset(adae, TRTEMFL == "Y" & AESEV == "SEVERE"))[, c(c("USUBJID", "AEDECOD")), drop = FALSE]CODE: OK (checked: the same rows)21.7 Viewer or program?
Section titled “21.7 Viewer or program?”| Use the data viewer to | Use a program to |
|---|---|
| learn a new data set: values, missing values, odd records | produce anything that goes into a deliverable |
| find the record behind a surprising number | count, summarise or derive |
| test a condition before you put it into a program | get a result that a second programmer can reproduce |
| look at the data at a stop (chapter 24) | document what you did |
The viewer helps you understand. The program is what you validate.
21.8 Summary
Section titled “21.8 Summary”- F12 opens the last data frame; Ctrl+Alt+D opens the one at the cursor. The data stays in R.
wfilters with an R condition,fcounts the values of a column, Ctrl+Alt+I picks values and writes the condition, Ctrl+Alt+R shows every row again.- The column filters take the short forms of DataStep Studio for SAS:
>65,65..80,a,b,.. NAis not a value. The viewer writesis.na()for it, and a removal keeps theNArows.- R code turns the view into dplyr code for your program.
dsr query,freq,values,filter,describeandsubjectgive the same answers in a terminal.
21.9 Exercises
Section titled “21.9 Exercises”Exercise 21-1. In adsl, keep the safety population (SAFFL == "Y") and use the column filter >80 under AGE. How many subjects are older than 80?
Exercise 21-2. In adae, open the value picker on ASTDTF (the imputation flag of the start date). How many records have an imputed start date? Which chapter of Part VI finds the same records in SDTM?
Exercise 21-3. In adae, tick Placebo and Xanomeline Low Dose in the value picker of TRT01A. Which R condition does the viewer write, and how many rows does it keep?
Exercise 21-4. With the WHERE of 21.3 and TRT01A == "Placebo", which subject has two records of the same event? What does that mean for the count of subjects?
The answers are in Answers, Part VI.
Found an error or have a question? Write to datastep.studio@gmail.com· Every program runs in DataStep Studio for R, the R extension for VS Code and Positron.
Data: the CDISC pilot study (cdiscpilot01), from the R packagespharmaversesdtm andpharmaverseadam (Apache License 2.0).
R is a project of the R Foundation. SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. This book is not affiliated with or endorsed by SAS Institute Inc. or the R Foundation. CDISC, SDTM and ADaM are trademarks of the Clinical Data Interchange Standards Consortium.
© 2026 DataStep Labs. All rights reserved.