How to create some sample data in SAS and R tidyverse

This post is part of 'SASnR | Creating sample data' series

When working on an analysis, we often want to create some example data to test our programming approaches.

In this post, we will see one approach of creating example data in SAS and R tidyverse.

SAS code

data test;
    infile cards;
    input usubjid$ lbtestcd$ lbstresn;
1001 HGB 12.5
1002 HGB 12.1
1003 HGB 13
  • data step of SAS is used to create the example data
  • infile statement is used to specify that the data is expected within the step using "cards" option
  • input statement is used to define the names and datatype of the variables
  • actual data is entered after the cards statement

R tidyverse code



  "1001", "HGB", 12.5,
  "1002", "HGB", 12.1,
  "1003", "HGB", 13,



  • tidyverse library is loaded into R session using library function
  • tribble function is used to create the example data
  • first row is used to specify the names of the variables/columns
  • column names are prefixed with a tilde(~) symbol
  • actual data is entered after the column names are defined, with one observation per line
  • character data is entered within quotes
  • R tidyverse automatically assigns the datatype based on the data. We can control/change the datatype using some advanced functions of dplyr.


Post categories